简书中比较全的多线程执行,在这里保存下链接传送门
//
// ViewController.m
// syn_asyn
//
// Created by mac on 2018/6/20.
// Copyright © 2018年 Gooou. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 使用 NSThread 的 detachNewThreadSelector 方法会创建线程,并自动启动线程执行 selector 任务,不会造成卡顿,任务三个都加在主线程中执行,而主线程没有任务执行,所以不会造成崩溃。
// [NSThread detachNewThreadSelector:@selector(sync_mainQueue) toTarget:self withObject:nil];
[self groupEnterAndLeave];
}
/*同步执行,并发队列*/
-(void)sync_ConcurrentQueue{
NSLog(@"currentThread--%@",[NSThread currentThread]);//打印当前线程
NSLog(@"syncConcurrent--begin");
//串行队列
dispatch_queue_t queue=dispatch_queue_create("net.bujige.testQueue", DISPATCH_QUEUE_CONCURRENT);
//同步执行
dispatch_sync(queue, ^{
//追加任务
for (int i=0; i<2; ++i) {
[NSThread sleepForTimeInterval:2];//模拟操作耗时
NSLog(@"1--%@",[NSThread currentThread]);//打印当前线程
}
});
dispatch_sync(queue, ^{
//追加任务2
for (int i=0; i<2; ++i) {
[NSThread sleepForTimeInterval:2];
NSLog(@"2--%@",[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{
//追加任务3
for (int i=0; i<2; ++i) {
[NSThread sleepForTimeInterval:2];
NSLog(@"3--%@",[NSThread currentThread]);
}
});
NSLog(@"syncConcurrent--end");
}
/*异步执行并发队列:可开启多个线程,任务交替执行*/
-(void)async_ConcurrentQueue{
/*异步执行+并发队列
特点:可以开启多个线程,任务交替执行
*/
NSLog(@"currentThread--%@",[NSThread currentThread]);
NSLog(@"asyncConcurrent--begin");
dispatch_queue_t queue=dispatch_queue_create("net.bujige.testQueue",DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
//追加任务
for (int i=0; i<2; ++i) {
[NSThread sleepForTimeInterval:2];//模拟操作耗时
NSLog(@"1--%@",[NSThread currentThread]);//打印当前线程
}
});
dispatch_async(queue, ^{
//追加任务2
for (int i=0; i<2; ++i) {
[NSThread sleepForTimeInterval:2];
NSLog(@"2--%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
//追加任务3
for (int i=0; i<2; ++i) {
[NSThread sleepForTimeInterval:2];
NSLog(@"3--%@",[NSThread currentThread]);
}
});
NSLog(@"asyncConcurrent--end");
}
/*同步执行+串行队列*/
-(void)sync_serialQueue{
NSLog(@"currentThread--%@",[NSThread currentThread]);
NSLog(@"syncConcurrent--begin");
dispatch_queue_t queue=dispatch_queue_create("net.bujige.testQueue",DISPATCH_QUEUE_SERIAL);
dispatch_sync(queue, ^{
//追加任务
for (int i=0; i<2; ++i) {
[NSThread sleepForTimeInterval:2];//模拟操作耗时
NSLog(@"1--%@",[NSThread currentThread]);//打印当前线程
}
});
dispatch_sync(queue, ^{
//追加任务2
for (int i=0; i<2; ++i) {
[NSThread sleepForTimeInterval:2];
NSLog(@"2--%@",[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{
//追加任务3
for (int i=0; i<2; ++i) {
[NSThread sleepForTimeInterval:2];
NSLog(@"3--%@",[NSThread currentThread]);
}
});
NSLog(@"syncConcurrent--end");
}
/*异步执行+串行队列*/
-(void)async_serialQueue{
NSLog(@"currentThread--%@",[NSThread currentThread]);
NSLog(@"asyncConcurrent--begin");
dispatch_queue_t queue=dispatch_queue_create("net.bujige.testQueue",DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
//追加任务
for (int i=0; i<2; ++i) {
[NSThread sleepForTimeInterval:2];//模拟操作耗时
NSLog(@"1--%@",[NSThread currentThread]);//打印当前线程
}
});
dispatch_async(queue, ^{
//追加任务2
for (int i=0; i<2; ++i) {
[NSThread sleepForTimeInterval:2];
NSLog(@"2--%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
//追加任务3
for (int i=0; i<2; ++i) {
[NSThread sleepForTimeInterval:2];
NSLog(@"3--%@",[NSThread currentThread]);
}
});
NSLog(@"asyncConcurrent--end");
}
/*同步执行+主队列
特点:互相等待卡主不执行
不会开启新线程,执行完一个任务,执行下一个任务
会产生崩溃
*/
-(void)sync_mainQueue{
NSLog(@"currentThread--%@",[NSThread currentThread]);
NSLog(@"syncMain--begin");
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]);//打印当前线程
}
});
dispatch_sync(queue, ^{
//追加任务2
for (int i=0; i<2; ++i) {
[NSThread sleepForTimeInterval:2];
NSLog(@"2--%@",[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{
//追加任务3
for (int i=0; i<2; ++i) {
[NSThread sleepForTimeInterval:2];
NSLog(@"3--%@",[NSThread currentThread]);
}
});
NSLog(@"syncMain--end");
}
/*异步执行+主队列
只在主线程执行任务,执行完一个任务,再执行下一个任务
所有任务都是在当前线程(主线程)中执行的,并没有开启新的线程(虽然异步执行具备开启线程的能力,但因为是主队列,所以所有任务都在主线程中)
*/
-(void)async_mainQueue{
NSLog(@"currentThread--%@",[NSThread currentThread]);
NSLog(@"asyncMain--begin");
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]);//打印当前线程
}
});
dispatch_async(queue, ^{
//追加任务2
for (int i=0; i<2; ++i) {
[NSThread sleepForTimeInterval:2];
NSLog(@"2--%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
//追加任务3
for (int i=0; i<2; ++i) {
[NSThread sleepForTimeInterval:2];
NSLog(@"3--%@",[NSThread currentThread]);
}
});
NSLog(@"asyncMain--end");
}
/*
线程间的通讯
*/
-(void)communication{
//获取全局并发队列
dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//获取主队列
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]);
});
});
}
/*
栅栏方法:需要异步执行两组操作,而且第一组操作完成之后,才能开始执行第二组操作,
*/
-(void)barrier{
dispatch_queue_t queue = dispatch_queue_create("net.bujige.testQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
// 追加任务1
for (int i = 0; i < 2; ++i) {
[NSThread sleepForTimeInterval:2]; // 模拟耗时操作
NSLog(@"1---%@",[NSThread currentThread]);// 打印当前线程
}
});
dispatch_async(queue, ^{
// 追加任务2
for (int i = 0; i < 2; ++i) {
[NSThread sleepForTimeInterval:2]; // 模拟耗时操作
NSLog(@"2---%@",[NSThread currentThread]);// 打印当前线程
}
});
dispatch_barrier_sync(queue, ^{
// 追加任务 barrier
for (int i = 0; i < 2; ++i) {
[NSThread sleepForTimeInterval:2]; // 模拟耗时操作
NSLog(@"barrier---%@",[NSThread currentThread]);// 打印当前线程
}
});
dispatch_async(queue, ^{
// 追加任务3
for (int i = 0; i < 2; ++i) {
[NSThread sleepForTimeInterval:2]; // 模拟耗时操作
NSLog(@"3---%@",[NSThread currentThread]);// 打印当前线程
}
});
dispatch_async(queue, ^{
// 追加任务4
for (int i = 0; i < 2; ++i) {
[NSThread sleepForTimeInterval:2]; // 模拟耗时操作
NSLog(@"4---%@",[NSThread currentThread]);// 打印当前线程
}
});
}
/*
延时执行方法
*/
-(void)after{
NSLog(@"currentThread--%@",[NSThread currentThread]);
NSLog(@"asyncMain--begin");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//2.0秒之后异步追加任务到主队列中,并开始执行
NSLog(@"after--%@",[NSThread currentThread]);
});
}
/*只执行一次
多线程环境下,保证线程安全
*/
-(void)once{
static dispatch_queue_t onceToken;
// dispatch_once(&onceToken, ^{
// //执行代码
// });
}
/*
dispatch_apply按照指定的次数将指定的任务添加到指定的队列中取,并等待全部队列执行结束
*/
-(void)apply{
dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
NSLog(@"apply--begin");
dispatch_apply(6, queue, ^(size_t index) {
NSLog(@"%zd--%@",index,[NSThread currentThread]);
});
NSLog(@"apply--end");
}
/*
分别异步执行2个耗时任务,然后当2个耗时任务执行完成之后回到主线程执行任务。
*/
-(void)groupNofity{
NSLog(@"currentThread---%@",[NSThread currentThread]); // 打印当前线程
NSLog(@"group---begin");
dispatch_group_t group=dispatch_group_create();
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 追加任务1
for (int i = 0; i < 2; ++i) {
[NSThread sleepForTimeInterval:2]; // 模拟耗时操作
NSLog(@"1---%@",[NSThread currentThread]); // 打印当前线程
}
});
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 追加任务2
for (int i = 0; i < 2; ++i) {
[NSThread sleepForTimeInterval:2]; // 模拟耗时操作
NSLog(@"2---%@",[NSThread currentThread]); // 打印当前线程
}
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
// 等前面的异步任务1、任务2都执行完毕后,回到主线程执行下边任务
for (int i = 0; i < 2; ++i) {
[NSThread sleepForTimeInterval:2]; // 模拟耗时操作
NSLog(@"3---%@",[NSThread currentThread]); // 打印当前线程
}
NSLog(@"group---end");
});
}
/*暂停当前线程,等待指定的group中的任务执行完成后,才会往下执行
*/
-(void)groupWait{
NSLog(@"currentThread---%@",[NSThread currentThread]); // 打印当前线程
NSLog(@"group---begin");
dispatch_group_t group=dispatch_group_create();
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 追加任务1
for (int i = 0; i < 2; ++i) {
[NSThread sleepForTimeInterval:2]; // 模拟耗时操作
NSLog(@"1---%@",[NSThread currentThread]); // 打印当前线程
}
});
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 追加任务1
for (int i = 0; i < 2; ++i) {
[NSThread sleepForTimeInterval:2]; // 模拟耗时操作
NSLog(@"2---%@",[NSThread currentThread]); // 打印当前线程
}
});
//等待上面的任务全部完成后,会往下继续执行(会阻塞当前线程)
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
NSLog(@"group---end");
}
- (void)groupEnterAndLeave
{
NSLog(@"currentThread---%@",[NSThread currentThread]); // 打印当前线程
NSLog(@"group---begin");
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_enter(group);
dispatch_async(queue, ^{
// 追加任务1
for (int i = 0; i < 2; ++i) {
[NSThread sleepForTimeInterval:2]; // 模拟耗时操作
NSLog(@"1---%@",[NSThread currentThread]); // 打印当前线程
}
dispatch_group_leave(group);
});
dispatch_group_enter(group);
dispatch_async(queue, ^{
// 追加任务2
for (int i = 0; i < 2; ++i) {
[NSThread sleepForTimeInterval:2]; // 模拟耗时操作
NSLog(@"2---%@",[NSThread currentThread]); // 打印当前线程
}
dispatch_group_leave(group);
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
// 等前面的异步操作都执行完毕后,回到主线程.
for (int i = 0; i < 2; ++i) {
[NSThread sleepForTimeInterval:2]; // 模拟耗时操作
NSLog(@"3---%@",[NSThread currentThread]); // 打印当前线程
}
NSLog(@"group---end");
});
}
@end