- // 串行隊列 分兩種
- // 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)(33 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- NSLog(@"延遲3秒執行");
- });
- // ull 是C語言的數值字面量 相當於 unsigned long long
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 55ull * 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(@"第一滴血");
- });
- }