ios GCD中的“全局队列”和“主队列”有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9602042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between the "global queue" and the "main queue" in GCD?
提问by Proud Member
Among some other ways, there are these two ways to get queues in GCD
:
在其他一些方法中,有以下两种方法可以获取队列GCD
:
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_get_main_queue();
If I'm not completely wrong, the "main queue" is executing on the main thread and is good for "callback" blocks which execute UI work.
如果我没有完全错,“主队列”正在主线程上执行,并且对于执行 UI 工作的“回调”块很有用。
Does this mean a "global queue" is one that runs on a background thread?
这是否意味着“全局队列”是在后台线程上运行的队列?
回答by mattjgalloway
The main queue does indeed run on the main thread like you say.
主队列确实像你说的那样在主线程上运行。
The global queues are concurrent queues and from the main page for dispatch_get_global_queue:
全局队列是并发队列,来自 dispatch_get_global_queue 的主页:
Unlike the main queue or queues allocated with dispatch_queue_create(), the global concurrent queues schedule blocks as soon as threads become available ("non-FIFO" completion order). The global concurrent queues represent three priority bands:
? DISPATCH_QUEUE_PRIORITY_HIGH ? DISPATCH_QUEUE_PRIORITY_DEFAULT ? DISPATCH_QUEUE_PRIORITY_LOW
Blocks submitted to the high priority global queue will be invoked before those submitted to the default or low priority global queues. Blocks submitted to the low priority global queue will only be invoked if no blocks are pending on the default or high priority queues.
与使用 dispatch_queue_create() 分配的主队列或队列不同,全局并发队列在线程可用时立即调度阻塞(“非 FIFO”完成顺序)。全局并发队列代表三个优先级带:
? DISPATCH_QUEUE_PRIORITY_HIGH ? DISPATCH_QUEUE_PRIORITY_DEFAULT ? DISPATCH_QUEUE_PRIORITY_LOW
提交到高优先级全局队列的块将在提交到默认或低优先级全局队列的块之前被调用。提交到低优先级全局队列的块只有在默认或高优先级队列上没有待处理的块时才会被调用。
So, they are queues which run on background threads as and when they become available. They're "non-FIFO" so ordering is not guaranteed.
因此,它们是在可用时在后台线程上运行的队列。它们是“非 FIFO”,因此无法保证订购。
回答by jjrscott
The 5 queues (4 background, 1 main) all have different thread priorities (-[NSThread threadPriority]
) too:
5 个队列(4 个后台,1 个主)也都有不同的线程优先级 ( -[NSThread threadPriority]
):
-main- : 0.758065
DISPATCH_QUEUE_PRIORITY_HIGH : 0.532258
DISPATCH_QUEUE_PRIORITY_DEFAULT : 0.500000
DISPATCH_QUEUE_PRIORITY_LOW : 0.467742
DISPATCH_QUEUE_PRIORITY_BACKGROUND : 0.000000
(tested on an iPod 4th gen and the simulator on a MacBook Pro)
(在第 4 代 iPod 和 MacBook Pro 模拟器上测试)
回答by Grzegorz Adam Hankiewicz
Yes. You can run code like this on a device to test it:
是的。您可以在设备上运行这样的代码来测试它:
dispatch_async(
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"Block 1a");
NSAssert(![NSThread isMainThread], @"Wrong thread!");
NSLog(@"Block 1b");
});
dispatch_async(
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Block 2a");
NSAssert([NSThread isMainThread], @"Wrong thread!");
NSLog(@"Block 2b");
});
});
回答by Lyju I Edwinson
Global dispatch queue :
全局调度队列:
- Tasks in concurrent queue executes concurrently [background threads]
- Tasks are still started in the order that they were added to the queue
- 并发队列中的任务并发执行【后台线程】
- 任务仍然按照它们被添加到队列的顺序启动
Main dispatch queue :
主要调度队列:
- Available serial queue that executes tasks on the application's main thread.
- It is usually called from a background queue when some background processing has finished and the user interface needs to be updated.
- 在应用程序的主线程上执行任务的可用串行队列。
- 当某些后台处理完成并且用户界面需要更新时,它通常从后台队列中调用。