您的位置:首页 > 其它

GCD的简单使用,开辟一条新的线程,让上面的任务串行执行

2016-07-12 14:55 567 查看
1. 异步线程+串行队列  新开辟一条线程。串行执行   

dispatch_queue_t chuanQue=dispatch_queue_create("chuan", DISPATCH_QUEUE_SERIAL);

    dispatch_async(chuanQue, ^{

       

        for (int i=0; i<50; i++) {

            NSLog(@"%@A线程的%d",[NSThread currentThread],i);

        }

    });

    dispatch_async(chuanQue, ^{

        for (int i=0; i<50; i++) {

            NSLog(@"%@B线程的%d",[NSThread currentThread],i);

        }

    });
2. 开辟一条异步线程,让他们并发执行
dispatch_async与并发队列配合

3. 下次并发队列我们不需要手动创建,直接使用系统自带的全局并发队列就可以了e

  // 1.获得全局的并发队列

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    

    // 2.将任务加入队列

    dispatch_async(queue, ^{

        for (NSInteger i = 0; i<10; i++) {

            NSLog(@"1-----%@", [NSThread currentThread]);

        }

    });

    dispatch_async(queue, ^{

        for (NSInteger i = 0; i<10; i++) {

            NSLog(@"2-----%@", [NSThread currentThread]);

        }

    });

    dispatch_async(queue, ^{

        for (NSInteger i = 0; i<10; i++) {

            NSLog(@"3-----%@", [NSThread currentThread]);

        }

    });
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: