iOS疯狂详解之GCD

来源:互联网 发布:arm linux gcc 4.9.1 编辑:程序博客网 时间:2024/05/22 05:32
    //  串行队列 分两种    //  1.主队列    //  创建一个主队列    dispatch_queue_t mainQueue = dispatch_get_main_queue();    //  像主队列中添加任务    //  参数1 要添加的队列    //  参数2 要添加的任务    dispatch_async(mainQueue, ^{        NSLog(@"第一个任务,所在线程:%@, 是否是主线程:%d", [NSThread currentThread], [NSThread currentThread].isMainThread);    });    dispatch_async(mainQueue, ^{        NSLog(@"第二个任务,所在线程:%@, 是否是主线程:%d", [NSThread currentThread], [NSThread currentThread].isMainThread);    });        dispatch_async(mainQueue, ^{        NSLog(@"第三个任务,所在线程:%@, 是否是主线程:%d", [NSThread currentThread], [NSThread currentThread].isMainThread);    });        //  任务延迟    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{                NSLog(@"延迟3秒执行");            });        //  ull 是C语言的数值字面量 相当于 unsigned long long    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5ull * NSEC_PER_SEC), mainQueue, ^{        NSLog(@"延迟5秒执行");    });        //  综上:串行主队列 都在主线冲中进行任务 结束一个 才能进入下一个


    //  2.自定义队列    //  创建一个队列    //  参数1 自定义队列的标示符 名字    //  参数2 自定义队列的种类 串行    dispatch_queue_t myQueue = dispatch_queue_create("com.wl.MyQueue", DISPATCH_QUEUE_SERIAL);    dispatch_async(myQueue, ^{        for (int i = 0; i < 10; i++) {            NSLog(@"第一个任务,所在线程:%@, 是否是主线程:%d-----%d", [NSThread currentThread], [NSThread currentThread].isMainThread,i);        }    });    dispatch_async(myQueue, ^{        for (int i = 10; i < 20; i++) {            NSLog(@"第二个任务,所在线程:%@, 是否是主线程:%d-----%d", [NSThread currentThread], [NSThread currentThread].isMainThread,i);        }    });    dispatch_async(myQueue, ^{        for (int i = 20; i < 30; i++) {            NSLog(@"第三个任务,所在线程:%@, 是否是主线程:%d-----%d", [NSThread currentThread], [NSThread currentThread].isMainThread,i);        }    });

 
    //  创建一个 并行队列    //  参数1 设置优先级 无优先级    //  参数2 预留参数 一般给0//    dispatch_queue_t myQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);//    dispatch_async(myQueue, ^{//        for (int i = 0; i < 10; i++) {//            NSLog(@"第一个任务,所在线程:%@, 是否是主线程:%d-----%d", [NSThread currentThread], [NSThread currentThread].isMainThread,i);//        }//    });//    dispatch_async(myQueue, ^{//        for (int i = 10; i < 20; i++) {//            NSLog(@"第二个任务,所在线程:%@, 是否是主线程:%d-----%d", [NSThread currentThread], [NSThread currentThread].isMainThread,i);//        }//    });//    dispatch_async(myQueue, ^{//        for (int i = 20; i < 30; i++) {//            NSLog(@"第三个任务,所在线程:%@, 是否是主线程:%d-----%d", [NSThread currentThread], [NSThread currentThread].isMainThread,i);//        }//    });            // 2.自定义队列,需要自己手动创建,并设置队列为并行    dispatch_queue_t myQueue = dispatch_queue_create("com.lanou3g.ConcurrentQueue", DISPATCH_QUEUE_CONCURRENT);        dispatch_async(myQueue, ^{        for (int i = 0; i < 10; i++) {            NSLog(@"第一个任务,所在线程:%@, 是否是主线程:%d~~~~~~~~~~~%d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);        }    });    dispatch_async(myQueue, ^{        for (int i = 10; i < 20; i++) {            NSLog(@"第二个任务,所在线程:%@, 是否是主线程:%d~~~~~~~~~~~%d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);        }    });    dispatch_async(myQueue, ^{        for (int i = 20; i < 30; i++) {            NSLog(@"第三个任务,所在线程:%@, 是否是主线程:%d~~~~~~~~~~~%d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);        }    });

//  分组队列- (void)group{    // 创建分组    dispatch_group_t group = dispatch_group_create();    // 创建并行队列(全局队列)    dispatch_queue_t myQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);    // 把myQueue添加到分组group中,并且给myQueue添加任务    dispatch_group_async(group, myQueue, ^{        for (int i = 0; i < 10; i++) {            NSLog(@"第一个任务,所在线程:%@, 是否是主线程:%d~~~~~~~~~~~%d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);        }    });    dispatch_group_async(group, myQueue, ^{        for (int i = 10; i < 20; i++) {            NSLog(@"第二个任务,所在线程:%@, 是否是主线程:%d~~~~~~~~~~~%d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);        }    });    // 在分组所有任务执行完成之后,最后指向下面的任务    dispatch_group_notify(group, myQueue, ^{        // 回到主线程刷新UI        dispatch_async(dispatch_get_main_queue(), ^{            NSLog(@"当前的线程是: %@", [NSThread currentThread]);            NSLog(@"所有数据下载完成.可以去刷新UI了");        });    });        dispatch_group_async(group, myQueue, ^{        for (int i = 20; i < 30; i++) {            NSLog(@"第三个任务,所在线程:%@, 是否是主线程:%d~~~~~~~~~~~%d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);        }    });}

- (void)barrier{    // 自己创建并行队列    dispatch_queue_t queue = dispatch_queue_create("com.lanou3g.Barrier", DISPATCH_QUEUE_CONCURRENT);    dispatch_async(queue, ^{        NSLog(@"玩家一读取完成");    });    dispatch_async(queue, ^{        NSLog(@"玩家二读取完成");    });    dispatch_async(queue, ^{        NSLog(@"玩家三读取完成");    });    // 设置屏障    dispatch_barrier_async(queue, ^{        NSLog(@"等待其他玩家进入...");    });        // 玩家一进入游戏    dispatch_async(queue, ^{        NSLog(@"玩家一进入游戏");    });    // 玩家二进入游戏    dispatch_async(queue, ^{        NSLog(@"玩家二进入游戏");    });    // 玩家三进入游戏    dispatch_async(queue, ^{        NSLog(@"玩家三进入游戏");    });    dispatch_barrier_async(queue, ^{        // 回到主线程,更新UI        dispatch_async(dispatch_get_main_queue(), ^{            NSLog(@"------%d", [NSThread currentThread].isMainThread);            NSLog(@"敌军即将在30秒后进入战场");        });    });        }//  只执行一次- (void)onceBlock{    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        NSLog(@"第一滴血");    });}



0 0