dispatch_apply 偶尔向主队列提交块

LinShiwei

我有以下代码:

dispatch_apply(10, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t index) {
    [NSThread sleepForTimeInterval:index];
    NSLog(@"sleep %lu, in thread %@",index,[NSThread currentThread]);
});

在主线程中运行它时,它会在控制台中打印:

2017-08-14 15:24:53.812 TestGCD[21480:4127352] sleep 0, in thread <NSThread: 0x600000067d40>{number = 1, name = main}
2017-08-14 15:24:54.816 TestGCD[21480:4127420] sleep 1, in thread <NSThread: 0x600000072100>{number = 3, name = (null)}
2017-08-14 15:24:55.816 TestGCD[21480:4127435] sleep 2, in thread <NSThread: 0x600000072180>{number = 4, name = (null)}
2017-08-14 15:24:56.815 TestGCD[21480:4127422] sleep 3, in thread <NSThread: 0x6080000731c0>{number = 5, name = (null)}
2017-08-14 15:24:57.813 TestGCD[21480:4127352] sleep 4, in thread <NSThread: 0x600000067d40>{number = 1, name = main}
2017-08-14 15:24:59.818 TestGCD[21480:4127420] sleep 5, in thread <NSThread: 0x600000072100>{number = 3, name = (null)}
2017-08-14 15:25:01.822 TestGCD[21480:4127435] sleep 6, in thread <NSThread: 0x600000072180>{number = 4, name = (null)}
2017-08-14 15:25:03.821 TestGCD[21480:4127422] sleep 7, in thread <NSThread: 0x6080000731c0>{number = 5, name = (null)}
2017-08-14 15:25:05.815 TestGCD[21480:4127352] sleep 8, in thread <NSThread: 0x600000067d40>{number = 1, name = main}
2017-08-14 15:25:08.824 TestGCD[21480:4127420] sleep 9, in thread <NSThread: 0x600000072100>{number = 3, name = (null)}

我发现在“sleep 0,4,8”中当前线程是主线程。

sleep 0, in thread <NSThread: 0x600000067d40>{number = 1, name = main}

为什么我将块提交到全局队列,但它偶尔会在主队列上调用?

此日志表明您必须dispatch_apply从主线程调用

  1. 当您说“将块提交到全局队列”时,请确保也将调用分派dispatch_apply到全局队列,因为这是一个同步 API。

  2. 当您将 调度dispatch_apply到某个后台队列时,请确保使用dispatch_async. 如果使用dispatch_sync它可以使用当前线程,作为优化。

你想要的是:

NSLog(@"starting on thread %@",[NSThread currentThread]);

dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), ^{
    dispatch_apply(10, dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), ^(size_t index) {
        [NSThread sleepForTimeInterval:index];
        NSLog(@"sleep %lu, in thread %@",index,[NSThread currentThread]);
    });
});

请注意,我正在使用dispatch_async并且将其dispatch_apply放入已调度块内,而不是相反。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

没有在模态运行循环中执行主队列上的dispatch_async块

dispatch_apply 留下一个线程“挂起”

dispatch_sync不适用于主队列

performSelectorOnMainThread:和主队列上的dispatch_async()有什么区别?

如何强制GCD在主队列上执行当前块?

从针对另一个调度队列的主线程发出dispatch_sync时,主队列/主线程会发生什么情况?

何时使用主队列

RNFetchBlob需要主队列设置

通过DispatchGroup与DispatchQueue访问主队列

主队列上异步任务的执行顺序

是否在主队列上触发IBAction?

为什么 DispatchGroup 会干扰主队列?

在节点之间平衡RabbitMQ主队列

在全局队列上用dispatch_async运行的块是否有可能在主线程上执行?

NSOperationQueue同步问题:辅助队列比主队列更新UI快

ios / swift调度队列-全局和主队列概念

了解dispatch_sync和全局队列

dispatch_after块未运行

GCD主队列和主线程有什么区别?

主队列相关查询的调度同步 [ios, swift]

在主队列上更改ViewController布局的最快方法?

如何在后台状态运行非主队列?

您如何等待主队列完成处理程序?

了解ios UIView动画块和dispatch_async(dispatch_get_main_queue)块

串行队列上的dispatch_async和dispatch_sync之间的区别?

在 dispatch_async 队列中的弱自我(dispatch_get_main_queue(),^{})

使用Objective-C中的大型中央dispatch / dispatch_async返回后台队列

从队列向 dask 客户端提交工作的最佳方法是什么?

iOS Core Data dispatch_async后台队列崩溃