閑來無事, 打開自己的印象筆記看到之前自己關(guān)于GCD/NSOperation/NSThread的筆記赔蒲,覺得很清晰易懂,于是曬出來:
NSOperation
自定義的任務(wù)繼承NSOperation 重寫main方法:
#import "Operation.h"
@implementation Operation
- (void)main
{
NSLog(@"任務(wù)即將開始執(zhí)行");
}
單例對象創(chuàng)建 dispatch_once 一次性操作:
/**
單例對象創(chuàng)建
*/
+ (instancetype) sharedInstance
{
static SingleInstance *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//只會調(diào)用一次
instance = [[self alloc] init];
});
return instance;
}
各種隊列:
// ViewController.m
// GCD-2
//
// Created by vera on 15/8/3.
// Copyright (c) 2015年 vera. All rights reserved.
//
#import "ViewController.h"
#import "Operation.h"
@interface ViewController ()
{
//串行隊列
dispatch_queue_t mainQueue;
//并行隊列
dispatch_queue_t globalQueue;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//串行隊列
mainQueue = dispatch_get_main_queue();
//并行隊列
globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
#if 0
//并行隊列
dispatch_queue_create("queueName", DISPATCH_QUEUE_CONCURRENT)
//串行隊列
dispatch_queue_create("queueName", DISPATCH_QUEUE_SERIAL);
#endif
設(shè)置隊列并行數(shù)访惜,-1表示無上限:
/*
隊列:先進(jìn)先出
棧:先進(jìn)后出
*/
/*
Operation *o = [[Operation alloc] init];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
queue.maxConcurrentOperationCount = 1;
[queue addOperation:o];
*/
有一個需求:需要將N個請求全部完成之后執(zhí)行某個操作該如何處理:dispatch_group_notify NSOperation使用addDenpendency來處理這個需求
- (void)有一個需求_需要將N個請求全部完成之后執(zhí)行某個操作_該如何處理
{
//調(diào)度組
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, globalQueue, ^{
NSLog(@"任務(wù)1開始");
[NSThread sleepForTimeInterval:3];
NSLog(@"任務(wù)1結(jié)束");
});
dispatch_group_async(group, globalQueue, ^{
NSLog(@"任務(wù)2開始");
[NSThread sleepForTimeInterval:10];
NSLog(@"任務(wù)2結(jié)束");
});
//必須放到最后,當(dāng)前面執(zhí)行的任務(wù)都執(zhí)行完才會執(zhí)行下面的任務(wù)
dispatch_group_notify(group, globalQueue, ^{
NSLog(@"上面的所有任務(wù)都完成");
});
}
主線程中同步調(diào)度嘹履,造成死鎖:
- (void)死鎖
{
NSLog(@"1111111");
//dispatch_sync 同步調(diào)度:就是把任務(wù)添加到指定隊列里面,然后必須等待任務(wù)完成才能繼續(xù)债热。
//dispatch_async 異步調(diào)度:就是把任務(wù)添加到指定隊列里面砾嫉,沒必須等待任務(wù)完成才能繼續(xù)。
//串行隊列
//并行隊列
//同步調(diào)度
//異步調(diào)度
//死鎖
dispatch_sync(mainQueue, ^{
NSLog(@"222222");
});
NSLog(@"333333");
連續(xù)調(diào)用某個任務(wù)多少次 dispatch_apply
- (void)test6
{
//連續(xù)調(diào)用多少次
dispatch_apply(10, globalQueue, ^(size_t time) {
NSLog(@"連續(xù)調(diào)用多少次 -- %zu",time);
});
}
延遲調(diào)用 dispatch_after
- (void)test5
{
//延遲delayInSeconds秒調(diào)用
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@" ----- 延遲3秒調(diào)用");
});
}
線程優(yōu)先級相關(guān)
A,B并發(fā)執(zhí)行 然后執(zhí)行C 最后D,E并發(fā)執(zhí)行
- (void)test4
{
/**
1.分割任務(wù)必須是自定義的隊列
2. dispatch_queue_create(隊列的標(biāo)簽, 串行/并行)
(1)DISPATCH_QUEUE_CONCURRENT;//并行
(2)DISPATCH_QUEUE_SERIAL //串行
*/
// dispatch_sync;系統(tǒng)沒有創(chuàng)建線程
// dispatch_async;系統(tǒng)創(chuàng)建線程
/**
創(chuàng)建一個自定義的并行隊列
*/
dispatch_queue_t customQueue = dispatch_queue_create("com.GCD.customQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(mainQueue, ^{
NSLog(@"################ %@",[NSThread currentThread]);
});
//任務(wù)A
dispatch_async(customQueue, ^{
[self doSomeOperation:@"A"];
NSLog(@"######### %@",[NSThread currentThread]);
});
//任務(wù)B
dispatch_async(customQueue, ^{
[self doSomeOperation:@"B"];
});
//分割窒篱,把前面的任務(wù)和后面的任務(wù)分開
//任務(wù)C
dispatch_barrier_async(customQueue, ^{
[self doSomeOperation:@"C"];
});
//任務(wù)D
dispatch_async(customQueue, ^{
[self doSomeOperation:@"D"];
});
//任務(wù)E
dispatch_async(customQueue, ^{
[self doSomeOperation:@"E"];
});
}
//A,B,C 3個任務(wù)焕刮,A,B并發(fā)執(zhí)行,最后執(zhí)行C
- (void)test3
{
//調(diào)度組
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, globalQueue, ^{
NSLog(@"任務(wù)1開始");
//NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://d3.s.hjfile.cn/2012/201202_3/43904b09-24e1-4fdb-8b46-d3dba3323278.mp3"]];
NSLog(@"任務(wù)1完成");
});
dispatch_group_async(group, globalQueue, ^{
NSLog(@"任務(wù)2開始");
// sleep(5);
NSLog(@"任務(wù)2完成");
});
//必須放到最后
dispatch_group_notify(group, globalQueue, ^{
//NSLog(@"任務(wù)3");
//1.開始請求數(shù)據(jù)
sleep(2);
NSLog(@"請求數(shù)據(jù)完成");
//2.回到主線程
dispatch_async(mainQueue, ^{
NSLog(@"回到主線程");
//刷新UI
});
//前面的任務(wù)都完成了墙杯。
// NSLog(@"---- %d",[NSThread isMainThread]);
});
}
//任務(wù)A,B,C并發(fā)執(zhí)行配并,無順序
- (void)test2
{
dispatch_async(globalQueue, ^{
//任務(wù)1
[self doSomeOperation:@"A"];
});
dispatch_async(globalQueue, ^{
//任務(wù)2
[self doSomeOperation:@"B"];
});
dispatch_async(globalQueue, ^{
//任務(wù)3
[self doSomeOperation:@"C"];
});
}
//任務(wù)A,B,C串行執(zhí)行
- (void)test1
{
/**
同步調(diào)度和異步調(diào)度
串行隊列和并行隊列
*/
dispatch_async(mainQueue, ^{
NSLog(@"---- %@",[NSThread currentThread]);
//任務(wù)1
[self doSomeOperation:@"A"];
});
NSLog(@"11111");
dispatch_async(mainQueue, ^{
//任務(wù)2
[self doSomeOperation:@"B"];
});
NSLog(@"22222");
dispatch_async(mainQueue, ^{
//任務(wù)3
[self doSomeOperation:@"C"];
});
NSLog(@"333333");
}
- (void)doSomeOperation:(NSString *)name
{
sleep(1);
NSLog(@"----- %@",name);
//NSData *data = [NSData dataWithContentsOfURL:<#(NSURL *)#>];
}