串行隊(duì)列
dispatch_queue_t queue = dispatch_queue_create("testQueue", DISPATCH_QUEUE_SERIAL);
并發(fā)隊(duì)列
dispatch_queue_t queue = dispatch_queue_create("testQueue2", DISPATCH_QUEUE_CONCURRENT);
主隊(duì)列
dispatch_queue_t mainQueue = dispatch_get_main_queue();
全局并發(fā)隊(duì)列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
同步執(zhí)行
dispatch_sync(queue, ^{
//這里放同步執(zhí)行任務(wù)代碼
});
異步執(zhí)行
dispatch_async(queue, ^{
//這里放異步執(zhí)行任務(wù)代碼
});
同步+并發(fā)隊(duì)列 = 沒有開啟新線程,串行執(zhí)行任務(wù) sync Concurrent
同步+串行隊(duì)列 = 沒有開啟新線程,串行執(zhí)行任務(wù) sync Serial
異步+并發(fā)隊(duì)列 = 有開啟新線程,并發(fā)執(zhí)行任務(wù) async Concurrent
異步+串行隊(duì)列 = 有開啟新線程(1條),串行執(zhí)行任務(wù) async Serial
同步+主隊(duì)列 = 主線程調(diào)用:死鎖 其他線程:沒有開啟新線程,串行執(zhí)行任務(wù)
異步+主隊(duì)列 = 沒有開啟新線程,串行執(zhí)行任務(wù)
同步+并發(fā)隊(duì)列(沒有開啟新線程,串行執(zhí)行任務(wù))
- (void)syncConcurrent {
dispatch_queue_t queue = dispatch_queue_create("syncConcurrent", DISPATCH_QUEUE_CONCURRENT);
dispatch_sync(queue, ^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2];//模擬耗時操作
NSLog(@"1--%@",[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"2--%@",[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"3--%@",[NSThread currentThread]);
}
});
NSLog(@"end");
}
同步+串行(沒有開啟新線程,串行執(zhí)行任務(wù))
- (void)syncSerial {
dispatch_queue_t queue = dispatch_queue_create("syncSerial", DISPATCH_QUEUE_SERIAL);
dispatch_sync(queue, ^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"1--%@",[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"2--%@",[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"3--%@",[NSThread currentThread]);
}
});
NSLog(@"end");
}
異步+并發(fā)(可開啟多個線程,任務(wù)交替(同時)執(zhí)行)
- (void)asyncConcurrent {
dispatch_queue_t queue = dispatch_queue_create("asyncConcurrent", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"1--%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"2--%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"3--%@",[NSThread currentThread]);
}
});
NSLog(@"end");
}
異步+串行(有開啟新線程(1條),串行執(zhí)行任務(wù))
- (void)asyncSerial {
dispatch_queue_t queue = dispatch_queue_create("asyncSerial", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"1--%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"2--%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"3--%@",[NSThread currentThread]);
}
});
NSLog(@"end");
}
同步執(zhí)行+主隊(duì)列(主線程調(diào)用:死鎖)
- (void)syncMain {
dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_sync(queue, ^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"1--%@",[NSThread currentThread]);
}
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"2--%@",[NSThread currentThread]);
}
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"3--%@",[NSThread currentThread]);
}
});
NSLog(@"end");
}
同步執(zhí)行+主隊(duì)列(其他線程調(diào)用:沒有開啟新線程,串行執(zhí)行任務(wù))
- (void)syncMain2 {
[NSThread detachNewThreadSelector:@selector(syncMain) toTarget:self withObject:nil];
}
異步執(zhí)行+主隊(duì)列(只在主線程中執(zhí)行任務(wù),執(zhí)行完一個任務(wù),再執(zhí)行下一個任務(wù))
- (void)asyncMain {
dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_async(queue, ^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"1--%@",[NSThread currentThread]);
}
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"2--%@",[NSThread currentThread]);
}
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"3--%@",[NSThread currentThread]);
}
});
NSLog(@"end");
}
GCD線程間的通信
- (void)communication {
//全局并發(fā)隊(duì)列
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
//主隊(duì)列
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_async(queue, ^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"1--%@",[NSThread currentThread]);
}
//回主線程
dispatch_async(mainQueue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"2--%@",[NSThread currentThread]);
});
});
}
柵欄方法 dispatch_barrier_async
- (void)barrier {
dispatch_queue_t queue = dispatch_queue_create("testQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"1--%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"2--%@",[NSThread currentThread]);
}
});
dispatch_barrier_async(queue, ^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"barrier--%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"3--%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"4--%@",[NSThread currentThread]);
}
});
}
GCD延時執(zhí)行方法: dispatch_after
- (void)after {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//3秒后執(zhí)行
NSLog(@"after---%@",[NSThread currentThread]);
});
}
GCD一次性代碼
-(void)once {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//只執(zhí)行一次的代碼(這里默認(rèn)是線程安全的)
});
}
GCD快速迭代
-(void)apply {
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
NSLog(@"apply begin");
dispatch_apply(6, queue, ^(size_t index) {
NSLog(@"%zd---%@",index, [NSThread currentThread]);
});
NSLog(@"apple end");
}
GCD隊(duì)列組 dispatch_group
dispatch_group_notify(監(jiān)聽group中任務(wù)完成的狀態(tài),當(dāng)所有的任務(wù)都執(zhí)行完成后,追加任務(wù)到group中,并執(zhí)行任務(wù))
- (void)groupNotify {
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"1--%@",[NSThread currentThread]);
}
});
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"2--%@",[NSThread currentThread]);
}
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"3--%@",[NSThread currentThread]);
}
});
}
dispatch_group_wait(暫停(阻塞)當(dāng)前線程,等待指定的group中的任務(wù)執(zhí)行完成后,才會往下繼續(xù)執(zhí)行)
- (void)groupWait {
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"1--%@",[NSThread currentThread]);
}
});
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2];
NSLog(@"2--%@",[NSThread currentThread]);
}
});
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
NSLog(@"group---end");
}
線程安全,使用semaphore加鎖
- (void)initTicketStatusSave {
semaphoreLock = dispatch_semaphore_create(1);
self.ticketCount = 100;
dispatch_queue_t queue1 = dispatch_queue_create("queue1", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t queue2 = dispatch_queue_create("queue2", DISPATCH_QUEUE_SERIAL);
__weak typeof(self) weakSelf = self;
dispatch_async(queue1, ^{
[weakSelf saleTicketNotSafe];
});
dispatch_async(queue2, ^{
[weakSelf saleTicketNotSafe];
});
}
- (void)saleTicketSafe {
while (1) {
//加鎖
dispatch_semaphore_wait(semaphoreLock, DISPATCH_TIME_FOREVER);
if (self.ticketCount > 0) {
self.ticketCount--;
NSLog(@"%@",[NSString stringWithFormat:@"剩余票數(shù) %ld 窗口:%@",(long)self.ticketCount,[NSThread currentThread]]);
} else {
NSLog(@"所有票已售完");
//相當(dāng)于解鎖
dispatch_semaphore_signal(semaphoreLock);
break;
}
//相當(dāng)于解鎖
dispatch_semaphore_signal(semaphoreLock);
}
}