主线程
作用
注意事项
IOS中得几种多线程实现方案
pThread
NSThread
GCD
NSOperation
其中GCD和NSOperation比较常用
// 多线程 pThread
pthread_t thread;
// 开启线程
pthread_create(&thread, NULL, run, NULL);
// 多线程 pThread
pthread_t thread1;
// 开启线程
pthread_create(&thread1, NULL, run, NULL);
void* run(void *para)
{
for (int i = 0 ;i < 10000; i ++) {
NSLog(@"run-%d----%@",i,[NSThread currentThread]);
}
return NULL;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
// NSThread
// 创建NSThread
NSThread *thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(threadRun:) object:@"鸟"];
NSThread *thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(threadRun:) object:@"蛋"];
NSThread *thread3 = [[NSThread alloc] initWithTarget:self selector:@selector(threadRun:) object:@"碎了"];
thread1.name = @"张三";
thread2.name = @"李四";
thread3.name = @"鸟";
// 开启线程
[thread1 start];
[thread2 start];
[thread3 start];
// 这种方法创建的线程无法获得线程对象,由系统管理
[NSThread detachNewThreadSelector:@selector(thread1:) toTarget:self withObject:@"狗蛋"];
}
- (void)threadRun:(id)obj
{
for (int i = 0 ;i < 100; i ++) {
NSLog(@"run-%d-%@---%@",i,obj,[NSThread currentThread]);
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
// 创建后台线程
[self performSelectorInBackground:@selector(thread1:) withObject:@"控制器"];
// 创建任务并添加到目标线程,可以是任意线程,子线程或者主线程
[self performSelector:@selector(thread2:) onThread:[NSThread mainThread] withObject:@"Main" waitUntilDone:NO];
}
- (void)thread1:(NSString *)str
{
for (int i = 0 ;i < 100; i ++) {
NSLog(@"run-%@---%@",str,[NSThread currentThread]);
}
}
- (void)thread2:(NSString *)str
{
for (int i = 0 ;i < 100; i ++) {
NSLog(@"run-%@---%@",str,[NSThread currentThread]);
}
}
- (void)thread1:(NSString *)str
{
for (int i = 0 ;i < 100; i ++) {
NSLog(@"run-%d-%@---%@",i,str,[NSThread currentThread]);
if (i == 19) {
sleep(2); // 当前线程睡眠2s
}
else if( i == 33 )
{
[NSThread sleepForTimeInterval:2]; // 睡眠2s
}
else if(i == 40)
{
// 获取当前时间延后2s后的时间
NSDate *endDate = [NSDate dateWithTimeIntervalSinceNow:2];
[NSThread sleepUntilDate:endDate]; // 根据日期睡眠线程
}
else if(i == 55)
{
NSLog(@"结束线程");
[NSThread exit]; // 结束线程
}
}
}
// 方法中对资源数的访问要加锁
- (void)saleTickets:(NSString *)str
{
// 三个消费者线程
while(1)
{
// 设置互斥锁
@synchronized (self)
{
// 获取资源数
NSInteger count = self.resourceCount;
if (count > 0) {
// 资源数减1
count -- ;
// 写入标记数据
self.resourceCount = count;
NSLog(@"%@消费了一个单位,还剩下%zd",[NSThread currentThread].name,count);
}
else
{
NSLog(@"资源全部使用完了");
break;
}
}
}
}
GCD中任务和队列
使用方式
并发队列+异步任务:创建多个线程,并发执行
/*
* 并发队列+异步任务:创建多个线程,并发执行
*/
- (void)concurrentAndAsync
{
// 创建一个并发队列
// 参数1:标识,一般用公司域名abc.com
// 参数2:队列类型:串行和并行两种
dispatch_queue_t queue = dispatch_queue_create("song.com", DISPATCH_QUEUE_CONCURRENT);
// 异步方式创建一个任务,任务不会立即执行
dispatch_async(queue, ^{
NSLog(@"1--%@--",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"2--%@--",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"3--%@--",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"4--%@--",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"5--%@--",[NSThread currentThread]);
});
// 先将以上任务全部添加完毕,然后这个函数结束,再开始并发执行线程
NSLog(@"dispatch_async--end -- %@--",[NSThread currentThread]);
// 执行结果
// dispatch_async--end -- <NSThread: 0x7ff3b1400b80>{number = 1, name = main}--
// 2--<NSThread: 0x7ff3b140bc40>{number = 2, name = (null)}--
// 1--<NSThread: 0x7ff3b16672e0>{number = 5, name = (null)}--
// 3--<NSThread: 0x7ff3b1556b20>{number = 3, name = (null)}--
// 4--<NSThread: 0x7ff3b165c760>{number = 4, name = (null)}--
// 5--<NSThread: 0x7ff3b1413aa0>{number = 6, name = (null)}--
}
/*
* 并发队列+同步任务:不会开启新线程,在父线程中同步执行各个子线程,也就是逐一执行,并且是添加过任务后立即执行,之后才能添加下一个任务
*/
- (void)concurrentAndSync
{
// 创建一个并发队列
// 参数1:标识,一般用公司域名abc.com
// 参数2:队列类型:串行和并行两种
dispatch_queue_t queue = dispatch_queue_create("song.com", DISPATCH_QUEUE_CONCURRENT);
// 同步方式创建任务,任务会立即执行,执行完成后才会继续向下执行
dispatch_sync(queue, ^{
NSLog(@"1--%@--",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"2--%@--",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"3--%@--",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"4--%@--",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"5--%@--",[NSThread currentThread]);
});
// 先将以上任务逐个依次执行完毕后才会执行这句话,并且多有任务的执行都在主线程里完成
NSLog(@"dispatch_sync--end -- %@--",[NSThread currentThread]);
// 输出结果如下
// 1--<NSThread: 0x7f9900711c90>{number = 1, name = main}--
// 2--<NSThread: 0x7f9900711c90>{number = 1, name = main}--
// 3--<NSThread: 0x7f9900711c90>{number = 1, name = main}--
// ......
// dispatch_sync--end -- <NSThread: 0x7f9900711c90>{number = 1, name = main}--
}
/*
* 串行队列+同步任务:不会开启新线程,在父线程中同步执行各个子线程,也就是逐一执行,并且是添加过任务后立即执行,之后才能添加下一个任务
*/
- (void)serialAndSync
{
// 创建一个串行队列
// 参数1:标识,一般用公司域名abc.com
// 参数2:队列类型:串行和并行两种
dispatch_queue_t queue = dispatch_queue_create("song.com", DISPATCH_QUEUE_SERIAL);
// 同步方式创建任务,任务会立即执行,执行完成后才会继续向下执行
dispatch_sync(queue, ^{
NSLog(@"1--%@--",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"2--%@--",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"3--%@--",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"4--%@--",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"5--%@--",[NSThread currentThread]);
});
// 先将以上任务逐个依次执行完毕后才会执行这句话,并且多有任务的执行都在主线程里完成
NSLog(@"dispatch_sync--end -- %@--",[NSThread currentThread]);
// 1--<NSThread: 0x7fdc38f188d0>{number = 1, name = main}--
// 2--<NSThread: 0x7fdc38f188d0>{number = 1, name = main}--
// 3--<NSThread: 0x7fdc38f188d0>{number = 1, name = main}--
// 4--<NSThread: 0x7fdc38f188d0>{number = 1, name = main}--
// 5--<NSThread: 0x7fdc38f188d0>{number = 1, name = main}--
// dispatch_sync--end -- <NSThread: 0x7fdc38f188d0>{number = 1, name = main}--
}
/*
* 串行队列+异步任务:创建新线程,但是只会创建一个新线程,所有的任务都是在这个子线程里执行,执行顺序按照添加任务 的先后顺序,并且不是立即执行,而是等整个方法
*/
- (void)serialAndAsync
{
// 创建一个串行队列
// 参数1:标识,一般用公司域名abc.com
// 参数2:队列类型:串行和并行两种
dispatch_queue_t queue = dispatch_queue_create("song.com", DISPATCH_QUEUE_SERIAL);
// 同步方式创建任务,任务会立即执行,执行完成后才会继续向下执行
dispatch_async(queue, ^{
NSLog(@"1--%@--",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"2--%@--",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"3--%@--",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"4--%@--",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"5--%@--",[NSThread currentThread]);
});
// 先将以上任务逐个依次执行完毕后才会执行这句话,并且多有任务的执行都在主线程里完成
NSLog(@"dispatch_async--end -- %@--",[NSThread currentThread]);
// dispatch_async--end -- <NSThread: 0x7f97f2611bf0>{number = 1, name = main}--
// 1--<NSThread: 0x7f97f24461b0>{number = 2, name = (null)}--
// 2--<NSThread: 0x7f97f24461b0>{number = 2, name = (null)}--
}
主队列 是GCD自带的一种特殊的串行队列
全局并发队列
主队列+异步任务:不会创建新线程,所有的任务都是在这个父线程里执行,执行顺序按照添加任务 的先后顺序,并且不是立即执行,而是等整个方法结束后依次执行
/*
* 主队列+异步任务:不会创建新线程,所有的任务都是在这个父线程里执行,执行顺序按照添加任务 的先后顺序,并且不是立即执行,而是等整个方法结束后依次执行 */
- (void)mainAndAsync
{
NSLog(@"dispatch_async--begin -- %@--",[NSThread currentThread]);
// 创建一个串行队列
// 参数1:标识,一般用公司域名abc.com
// 参数2:队列类型:串行和并行两种
dispatch_queue_t queue = dispatch_get_main_queue();;
// 异步方式创建任务,任务不会立即执行
dispatch_async(queue, ^{
NSLog(@"1--%@--",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"2--%@--",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"3--%@--",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"4--%@--",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"5--%@--",[NSThread currentThread]);
});
// 先将以上任务逐个依次执行完毕后才会执行这句话,并且多有任务的执行都在主线程里完成
NSLog(@"dispatch_get_main_queue--end -- %@--",[NSThread currentThread]);
}
/*
* 主队列+同步任务:这个会产生问题,死锁,添加任务到主队列的任务要求立即执行,但是主队列是串行队列,当前任务要求执行完当前任务在执行新添加的任务。结果就是:两个任务互相等待,产生死锁
*/
- (void)mainAndSync
{
NSLog(@"dispatch_sync--begin -- %@--",[NSThread currentThread]);
// 创建一个串行队列
// 参数1:标识,一般用公司域名abc.com
// 参数2:队列类型:串行和并行两种
dispatch_queue_t queue = dispatch_get_main_queue();
// 同步方式创建任务,任务会立即执行,执行完成后才会继续向下执行
dispatch_sync(queue, ^{
NSLog(@"1--%@--",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"2--%@--",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"3--%@--",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"4--%@--",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"5--%@--",[NSThread currentThread]);
});
// 先将以上任务逐个依次执行完毕后才会执行这句话,并且多有任务的执行都在主线程里完成
NSLog(@"dispatch_get_main_queue--end -- %@--",[NSThread currentThread]);
}
- (void)delay
{
// 延时执行
NSLog(@"start--time:%@",[NSDate date]);
// NSObject 方法
[self performSelector:@selector(run) withObject:nil afterDelay:2];
// NSTimer
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(run) userInfo:nil repeats:NO];
// GCD
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"run--time:%@",[NSDate date]);
});
}
- (void)excuteOnce
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@"只执行一次---%@",[NSThread currentThread]);
});
NSLog(@"excuteOnce--%@",[NSThread currentThread]);
}
// 快速迭代,顺序不确定
- (void)apply
{
dispatch_apply(10, dispatch_get_global_queue(0, 0), ^(size_t index) {
NSLog(@"dispatch_apply--%ld--%@",index,[NSThread currentThread]);
});
}
/*
* 队列组:先执行async里的任务,最后执行notify任务
*/
- (void)groupAndAsync
{
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 先执行3个耗时操作
dispatch_group_async(group, queue, ^{
for (int i = 0 ; i < 1000; i ++) {
NSLog(@"1--%@--",[NSThread currentThread]);
}
});
dispatch_group_async(group, queue, ^{
for (int i = 0 ; i < 1000; i ++) {
NSLog(@"2--%@--",[NSThread currentThread]);
}
});
dispatch_group_async(group, queue, ^{
for (int i = 0 ; i < 1000; i ++) {
NSLog(@"3--%@--",[NSThread currentThread]);
}
});
// 等到以上任务完成后才会执行这个notify任务
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"main--%@--",[NSThread currentThread]);
});
}
1、NSInvocationOperation
NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run) object:nil];
[op start];
2、NSBlockOperation
NSBlockOperation *ope1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"0---%@",[NSThread currentThread]);
}];
[ope1 addExecutionBlock:^{
NSLog(@"1---%@",[NSThread currentThread]);
}];
[ope1 addExecutionBlock:^{
NSLog(@"2---%@",[NSThread currentThread]);
}];
[ope1 addExecutionBlock:^{
NSLog(@"3---%@",[NSThread currentThread]);
}];
[ope1 start];
3、NSOperationQueue
// 添加到队列的任务会自动并发执行
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSBlockOperation *ope1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"1---%@",[NSThread currentThread]);
}];
NSBlockOperation *ope2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"2---%@",[NSThread currentThread]);
}];
NSBlockOperation *ope3 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"3---%@",[NSThread currentThread]);
}];
// 添加到队列会自动执行,不需要手动调用start方法
[queue addOperation:ope1];
[queue addOperation:ope2];
[queue addOperation:ope3];
[queue addOperationWithBlock:^{
NSLog(@"4---%@",[NSThread currentThread]);
}];
4、最大并发数
// 最大并发数,同一时刻只会有两条线程在执行
queue.maxConcurrentOperationCount = 2;
5、任务依赖 addDependency
// 添加依赖:任务1和2的执行在3之前
[ope3 addDependency:ope1];
[ope3 addDependency:ope2];
6、线程通信
// 获取主队列
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"main---%@",[NSThread currentThread]);
}];