GCD 线程的应用

来源:互联网 发布:php继承一个普通类 编辑:程序博客网 时间:2024/05/19 19:31

//用异步函数往并发队列中添加任务    //获得全局的并发队列    dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);    dispatch_async(queue, ^{        NSLog(@"1----------%@",[NSThread currentThread]);    });    dispatch_async(queue, ^{        NSLog(@"2----------%@",[NSThread currentThread]);    });    dispatch_async(queue, ^{        NSLog(@"3----------%@",[NSThread currentThread]);    });            //用异步函数往串行队列中添加任务         //第一个参数是串行队列的名称,是c语言的字符串         //第二个参数是队列的睡醒,一般来说串行队列不需要复制任何属性    dispatch_queue_t queue2 =dispatch_queue_create("zc", NULL);    //添加任务到队列中执行    dispatch_async(queue2, ^{        NSLog(@"1----------%@",[NSThread currentThread]);    });    dispatch_async(queue2, ^{        NSLog(@"2----------%@",[NSThread currentThread]);    });    dispatch_async(queue2, ^{        NSLog(@"3----------%@",[NSThread currentThread]);    });



    //获取主队列    dispatch_queue_t queue =dispatch_get_main_queue();    //把任务添加到主队列    dispatch_async(queue, ^{         NSLog(@"1----------%@",[NSThread currentThread]);    });    dispatch_async(queue, ^{         NSLog(@"2----------%@",[NSThread currentThread]);    });    dispatch_async(queue, ^{         NSLog(@"3----------%@",[NSThread currentThread]);    });


参考文章

http://www.cnblogs.com/wendingding/p/3805841.html

0 0