美文网首页
YYDispatchQueuePool(ing)

YYDispatchQueuePool(ing)

作者: Code_人生 | 来源:发表于2019-09-30 14:34 被阅读0次
    • 查看CPU的核心数:sysctl -n hw.logicalcpu

    一、线程爆炸

        //CPU 时间片的轮转实现“并发”,一核,资源消耗的!!!
        for (int i = 0; i < 1000; i++) {
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    //            [NSThread sleepForTimeInterval:1];
                NSLog(@"DYZ%d,%@",i,[NSThread currentThread]);
            });
        }
    

    二、queuePriority

    • maxConcurrentOperationCount = 1;
        _queue = [NSOperationQueue new];
        _queue.maxConcurrentOperationCount = 1;
        //优先级:较高优先级的任务获取调度的机会大很多~
        for (int i = 0; i < 20; i++) {
            NSBlockOperation *blockOp = [NSBlockOperation blockOperationWithBlock:^{
                NSLog(@"LG%d,%@",i,[NSThread currentThread]);
            }];
            blockOp.queuePriority = (i % 2 == 0 ? NSOperationQueuePriorityLow : NSOperationQueuePriorityVeryHigh);
            [_queue addOperation:blockOp];
        }
    
    2019-09-18 14:21:49.931165+0800 LGRunLoopTest[2455:828828] LG0,<NSThread: 0x6000030f0300>{number = 3, name = (null)}
    2019-09-18 14:21:49.931492+0800 LGRunLoopTest[2455:828830] LG1,<NSThread: 0x6000030d5040>{number = 4, name = (null)}
    2019-09-18 14:21:49.931660+0800 LGRunLoopTest[2455:828830] LG3,<NSThread: 0x6000030d5040>{number = 4, name = (null)}
    2019-09-18 14:21:49.931815+0800 LGRunLoopTest[2455:828828] LG5,<NSThread: 0x6000030f0300>{number = 3, name = (null)}
    2019-09-18 14:21:49.931966+0800 LGRunLoopTest[2455:828828] LG7,<NSThread: 0x6000030f0300>{number = 3, name = (null)}
    2019-09-18 14:21:49.932108+0800 LGRunLoopTest[2455:828828] LG9,<NSThread: 0x6000030f0300>{number = 3, name = (null)}
    2019-09-18 14:21:49.932252+0800 LGRunLoopTest[2455:828828] LG11,<NSThread: 0x6000030f0300>{number = 3, name = (null)}
    2019-09-18 14:21:49.932370+0800 LGRunLoopTest[2455:828828] LG13,<NSThread: 0x6000030f0300>{number = 3, name = (null)}
    2019-09-18 14:21:49.932509+0800 LGRunLoopTest[2455:828828] LG15,<NSThread: 0x6000030f0300>{number = 3, name = (null)}
    2019-09-18 14:21:49.932676+0800 LGRunLoopTest[2455:828828] LG17,<NSThread: 0x6000030f0300>{number = 3, name = (null)}
    2019-09-18 14:21:49.932822+0800 LGRunLoopTest[2455:828828] LG19,<NSThread: 0x6000030f0300>{number = 3, name = (null)}
    2019-09-18 14:21:49.945427+0800 LGRunLoopTest[2455:828828] LG2,<NSThread: 0x6000030f0300>{number = 3, name = (null)}
    2019-09-18 14:21:49.945520+0800 LGRunLoopTest[2455:828828] LG4,<NSThread: 0x6000030f0300>{number = 3, name = (null)}
    2019-09-18 14:21:49.945631+0800 LGRunLoopTest[2455:828828] LG6,<NSThread: 0x6000030f0300>{number = 3, name = (null)}
    2019-09-18 14:21:49.945730+0800 LGRunLoopTest[2455:828828] LG8,<NSThread: 0x6000030f0300>{number = 3, name = (null)}
    2019-09-18 14:21:49.945848+0800 LGRunLoopTest[2455:828830] LG10,<NSThread: 0x6000030d5040>{number = 4, name = (null)}
    2019-09-18 14:21:49.945970+0800 LGRunLoopTest[2455:828830] LG12,<NSThread: 0x6000030d5040>{number = 4, name = (null)}
    2019-09-18 14:21:49.946069+0800 LGRunLoopTest[2455:828830] LG14,<NSThread: 0x6000030d5040>{number = 4, name = (null)}
    2019-09-18 14:21:49.946151+0800 LGRunLoopTest[2455:828830] LG16,<NSThread: 0x6000030d5040>{number = 4, name = (null)}
    2019-09-18 14:21:49.946251+0800 LGRunLoopTest[2455:828830] LG18,<NSThread: 0x6000030d5040>{number = 4, name = (null)}
    
    • maxConcurrentOperationCount = 10;
        _queue = [NSOperationQueue new];
        _queue.maxConcurrentOperationCount = 10;
        //优先级:较高优先级的任务获取调度的机会大很多~
        for (int i = 0; i < 20; i++) {
            NSBlockOperation *blockOp = [NSBlockOperation blockOperationWithBlock:^{
                NSLog(@"LG%d,%@",i,[NSThread currentThread]);
            }];
            blockOp.queuePriority = (i % 2 == 0 ? NSOperationQueuePriorityLow : NSOperationQueuePriorityVeryHigh);
            [_queue addOperation:blockOp];
        }
    
    2019-09-18 14:24:45.671769+0800 LGRunLoopTest[2477:842191] LG3,<NSThread: 0x600002769280>{number = 6, name = (null)}
    2019-09-18 14:24:45.671785+0800 LGRunLoopTest[2477:842189] LG1,<NSThread: 0x60000275ba40>{number = 4, name = (null)}
    2019-09-18 14:24:45.671791+0800 LGRunLoopTest[2477:842190] LG2,<NSThread: 0x6000027547c0>{number = 5, name = (null)}
    2019-09-18 14:24:45.671800+0800 LGRunLoopTest[2477:840823] LG0,<NSThread: 0x60000277a640>{number = 3, name = (null)}
    2019-09-18 14:24:45.671822+0800 LGRunLoopTest[2477:842192] LG4,<NSThread: 0x6000027724c0>{number = 7, name = (null)}
    2019-09-18 14:24:45.671860+0800 LGRunLoopTest[2477:842194] LG6,<NSThread: 0x600002769080>{number = 9, name = (null)}
    2019-09-18 14:24:45.671849+0800 LGRunLoopTest[2477:842193] LG5,<NSThread: 0x60000277a000>{number = 8, name = (null)}
    2019-09-18 14:24:45.671910+0800 LGRunLoopTest[2477:842195] LG7,<NSThread: 0x600002779740>{number = 10, name = (null)}
    2019-09-18 14:24:45.672045+0800 LGRunLoopTest[2477:842196] LG8,<NSThread: 0x6000027549c0>{number = 11, name = (null)}
    2019-09-18 14:24:45.672071+0800 LGRunLoopTest[2477:842192] LG11,<NSThread: 0x6000027724c0>{number = 7, name = (null)}
    2019-09-18 14:24:45.672100+0800 LGRunLoopTest[2477:842191] LG9,<NSThread: 0x600002769280>{number = 6, name = (null)}
    2019-09-18 14:24:45.672089+0800 LGRunLoopTest[2477:840823] LG15,<NSThread: 0x60000277a640>{number = 3, name = (null)}
    2019-09-18 14:24:45.672117+0800 LGRunLoopTest[2477:842194] LG17,<NSThread: 0x600002769080>{number = 9, name = (null)}
    2019-09-18 14:24:45.672129+0800 LGRunLoopTest[2477:842190] LG13,<NSThread: 0x6000027547c0>{number = 5, name = (null)}
    2019-09-18 14:24:45.672132+0800 LGRunLoopTest[2477:842193] LG19,<NSThread: 0x60000277a000>{number = 8, name = (null)}
    2019-09-18 14:24:45.672161+0800 LGRunLoopTest[2477:842189] LG10,<NSThread: 0x60000275ba40>{number = 4, name = (null)}
    2019-09-18 14:24:45.672596+0800 LGRunLoopTest[2477:842195] LG12,<NSThread: 0x600002779740>{number = 10, name = (null)}
    2019-09-18 14:24:45.672810+0800 LGRunLoopTest[2477:842197] LG14,<NSThread: 0x60000271da00>{number = 12, name = (null)}
    2019-09-18 14:24:45.672976+0800 LGRunLoopTest[2477:842191] LG16,<NSThread: 0x600002769280>{number = 6, name = (null)}
    2019-09-18 14:24:45.673272+0800 LGRunLoopTest[2477:840823] LG18,<NSThread: 0x60000277a640>{number = 3, name = (null)}
    

    相关文章

      网友评论

          本文标题:YYDispatchQueuePool(ing)

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