关于GCD中的线程数

作者: 微微笑的蜗牛 | 来源:发表于2016-07-27 17:02 被阅读1056次

    前言

    gcd用起来很方便,虽说是由系统去控制线程的创建,销毁,调配。我们无需操心。但是滥用的话也会引起线程数量过多,系统开销过大,同时引入的问题也会更多,不好调试。所以需要合理的使用gcd,保持app的线程数量在合理范围之内。比如日志,数据库操作都在固定的线程中。

    那么,下面就来简单讨论下gcd下线程的创建情况。

    初步结论

    1. dispatch_sync,同步操作,派发到任何队列,都不会开启新线程。但是注意不要dispatch到当前队列中,否则会引发死锁。因为sync本身是同步操作,会阻塞当前的线程,所以另外新开线程也没有意义,会在当前线程中执行task。但如果是派发到主线程的,还是会回到主线程中去执行

    2. dispatch_async,异步操作,派发自己创建的串行队列中,会创建一个线程。如果派发到主线程中,则会在主线程中执行。

    3. dispatch_async到并发队列中,会创建线程。但同时并行的线程数是由系统当前的负载和cpu核心数来决定的。

    这里说的创建线程,其实也不太准确,并不一定100%会创建,如果线程池中有可满足当前需求的线程,就不会创建了。

    系统管理了一个线程池,经测试,最大开启的线程数为66个。其中串行队列的线程和并发队列的线程是分开的,比如串行队列用了thread1,并发队列是不会用到thread1的。

    dispatch_sync

    贴段代码:

    - (void)testSyncSerialQueue {
        NSLog(@"main thread = %@", [NSThread mainThread]);
    
        dispatch_queue_t serialQueue = dispatch_queue_create("com.summer.serial", 0);
    
        dispatch_sync(serialQueue, ^{
            NSLog(@"1,current thread = %@", [NSThread currentThread]);
        });
    
        dispatch_sync(serialQueue, ^{
            NSLog(@"2,current thread = %@", [NSThread currentThread]);
        });
    
        dispatch_queue_t serialQueue2 = dispatch_queue_create("com.summer.serial2", 0);
    
        dispatch_sync(serialQueue2, ^{
            NSLog(@"11,current thread = %@", [NSThread currentThread]);
        });
    }
    

    结果为:

    2016-07-27 16:20:41.109 GCDQueue[3426:1181884] main thread = <NSThread: 0x7fb671d06660>{number = 1, name = main}
    2016-07-27 16:20:41.109 GCDQueue[3426:1181884] 1,current thread = <NSThread: 0x7fb671d06660>{number = 1, name = main}
    2016-07-27 16:20:41.109 GCDQueue[3426:1181884] 2,current thread = <NSThread: 0x7fb671d06660>{number = 1, name = main}
    2016-07-27 16:20:41.110 GCDQueue[3426:1181884] 11,current thread = <NSThread: 0x7fb671d06660>{number = 1, name = main}
    

    我们dispatch_sync一些task到串行队列中,但是执行却都是在当前队列中。说明dispatch_sync都是在当前线程中执行的。

    dispatch_async到串行队列

    - (void)testAsyncSerialQueue {
        NSLog(@"main thread = %@", [NSThread mainThread]);
    
        // serialQueue1
        dispatch_queue_t serialQueue = dispatch_queue_create("com.summer.serial", DISPATCH_QUEUE_SERIAL);
    
        dispatch_async(serialQueue, ^{
            NSLog(@"1, dispatch_async serialQueue1, current thread = %@", [NSThread currentThread]);
        });
    
        dispatch_async(serialQueue, ^{
            NSLog(@"2, dispatch_async serialQueue1, current thread = %@", [NSThread currentThread]);
        });
    
        // serialQueue2
        dispatch_queue_t serialQueue2 = dispatch_queue_create("com.summer.serial2", DISPATCH_QUEUE_SERIAL);
    
        dispatch_async(serialQueue2, ^{
            NSLog(@"11, dispatch_async serialQueue2, current thread = %@", [NSThread currentThread]);
        });
    
        dispatch_async(serialQueue2, ^{
            NSLog(@"22, dispatch_async serialQueue2, current thread = %@", [NSThread currentThread]);
        });
    }
    

    输出结果:

    2016-07-27 16:31:44.364 GCDQueue[3550:1237427] main thread = <NSThread: 0x7f81d1506800>{number = 1, name = main}
    2016-07-27 16:31:44.365 GCDQueue[3550:1238329] 11, dispatch_async serialQueue2, current thread = <NSThread: 0x7f81d1784aa0>{number = 3, name = (null)}
    2016-07-27 16:31:44.365 GCDQueue[3550:1238321] 1, dispatch_async serialQueue1, current thread = <NSThread: 0x7f81d17852b0>{number = 2, name = (null)}
    2016-07-27 16:31:44.365 GCDQueue[3550:1238329] 22, dispatch_async serialQueue2, current thread = <NSThread: 0x7f81d1784aa0>{number = 3, name = (null)}
    2016-07-27 16:31:44.365 GCDQueue[3550:1238321] 2, dispatch_async serialQueue1, current thread = <NSThread: 0x7f81d17852b0>{number = 2, name = (null)}
    
    

    结果可以看出,每个串行队列创建了一个线程。

    dispatch_async到并行队列

    - (void)testAsyncConcurrentQueue {
    
        NSLog(@"main thread = %@", [NSThread mainThread]);
    
        // concurrentQueue1
        dispatch_queue_t concurrentQueue = dispatch_queue_create("com.summer.concurrent1", DISPATCH_QUEUE_CONCURRENT);
    
        dispatch_async(concurrentQueue, ^{
            NSLog(@"1, dispatch_async concurrentQueue1, current thread = %@", [NSThread currentThread]);
        });
    
        dispatch_async(concurrentQueue, ^{
            NSLog(@"2, dispatch_async concurrentQueue1, current thread = %@", [NSThread currentThread]);
        });
    
        dispatch_async(concurrentQueue, ^{
            NSLog(@"3, dispatch_async concurrentQueue1, current thread = %@", [NSThread currentThread]);
        });
    
        dispatch_async(concurrentQueue, ^{
            NSLog(@"4, dispatch_async concurrentQueue1, current thread = %@", [NSThread currentThread]);
        });
    
        dispatch_async(concurrentQueue, ^{
            NSLog(@"5, dispatch_async concurrentQueue1, current thread = %@", [NSThread currentThread]);
        });
    
        // concurrentQueue2
        dispatch_queue_t concurrentQueue2 = dispatch_queue_create("com.summer.concurrent2", DISPATCH_QUEUE_CONCURRENT);
    
        dispatch_async(concurrentQueue2, ^{
            NSLog(@"11, dispatch_async concurrentQueue2, current thread = %@", [NSThread currentThread]);
        });
    
        dispatch_async(concurrentQueue2, ^{
            NSLog(@"22, dispatch_async concurrentQueue2, current thread = %@", [NSThread currentThread]);
        });
    
        dispatch_async(concurrentQueue2, ^{
            NSLog(@"33, dispatch_async concurrentQueue2, current thread = %@", [NSThread currentThread]);
        });
    }
    
    2016-07-27 16:35:46.697 GCDQueue[3603:1259460] main thread = <NSThread: 0x7fe02ad01f30>{number = 1, name = main}
    2016-07-27 16:35:46.698 GCDQueue[3603:1260034] 2, dispatch_async concurrentQueue1, current thread = <NSThread: 0x7fe02ac28a40>{number = 3, name = (null)}
    2016-07-27 16:35:46.698 GCDQueue[3603:1260045] 1, dispatch_async concurrentQueue1, current thread = <NSThread: 0x7fe02ac1f370>{number = 2, name = (null)}
    2016-07-27 16:35:46.698 GCDQueue[3603:1260111] 3, dispatch_async concurrentQueue1, current thread = <NSThread: 0x7fe02ac1c060>{number = 4, name = (null)}
    2016-07-27 16:35:46.698 GCDQueue[3603:1260112] 4, dispatch_async concurrentQueue1, current thread = <NSThread: 0x7fe02ac03c50>{number = 5, name = (null)}
    2016-07-27 16:35:46.698 GCDQueue[3603:1260034] 5, dispatch_async concurrentQueue1, current thread = <NSThread: 0x7fe02ac28a40>{number = 3, name = (null)}
    2016-07-27 16:35:46.698 GCDQueue[3603:1260113] 11, dispatch_async concurrentQueue2, current thread = <NSThread: 0x7fe02ae06210>{number = 6, name = (null)}
    2016-07-27 16:35:46.698 GCDQueue[3603:1260045] 22, dispatch_async concurrentQueue2, current thread = <NSThread: 0x7fe02ac1f370>{number = 2, name = (null)}
    2016-07-27 16:35:46.699 GCDQueue[3603:1260111] 33, dispatch_async concurrentQueue2, current thread = <NSThread: 0x7fe02ac1c060>{number = 4, name = (null)}
    
    

    细看一下,在输出2和5时,用的是同一个线程。所以说dispatch_async不一定就会创建线程。

    暴力开启

    for (int i = 0; i < 2000; i++) {
            dispatch_async(concurrentQueue, ^{
                NSLog(@"%d, for in dispatch_async concurrentQueue1, current thread = %@", i, [NSThread currentThread]);
            });
        }
    

    丢了2000个task进去,测试系统最多开启66个线程。

    在stackoverflow找到个类似的提问。但是apple没有文档说明过。http://stackoverflow.com/questions/7213845/number-of-threads-created-by-gcd

    大致是说gcd的线程池最大64个,加上主线程和其他的线程为66个。gcd会动态的创建更多的线程,在当前的任务被阻塞的时候。所以要尽量避免block当前任务。

    相关文章

      网友评论

        本文标题:关于GCD中的线程数

        本文链接:https://www.haomeiwen.com/subject/hbiojttx.html