2019-08-07 iOS多线程开发

iOS开发常常会碰到多线程开发技术,我们常用的集中多线程开发的技术有一下几点:
(1)pThread进行开发,pthread是基于C语言;
(2)NSThread 进行开辟多线程方式;
(3)GCD ;
(4)NSOperation.

接下来对上面几种多线程技术进行详细的讲解:
一.pThread
(1) pThread的创建线程方式
pthread_t thread;
pthread_create(&thread, NULL, run, NULL);
//run是需要在子线程实现的方法。
void *run(void *data){
NSLog(@"我在子线程中执行……");
for (int i = 1; i < 10 ; i ++) {
NSLog(@"%d",i);
sleep(1);
}
return NULL;
}
2.NSThread 创建线程有三种方式
(1)方式一 NSThread *thread1 = [[NSThread alloc] initWithTarget:self
selector:@selector(runNSThread1) object:nil];
thread1.name = @"Name_thread1"; //设置线程的识别名
[thread1 setThreadPriority:0.7]; //设置优先级别
[thread1 start];
//runNSThread1 是在子线程里,执行的方法。
在子线程里,怎么回到主线程的方法需要调用 [self performSelectorOnMainThread:@selector(runMainThread) withObject:nil waitUntilDone:YES];回到主线程执行的方法runMainThread这个方法内部就是在主线程执行了。
(2)方式二 通过detachNewThreadSelector方式创建并执行线程
[NSThread detachNewThreadSelector:@selector(runNSThread1) toTarget:self withObject:nil];
(3)方式三 通过performSelectorInBackground方式创建并执行线程
[self performSelectorInBackground:@selector(runNSThread1) withObject:nil];

获取当前线程的信息 [NSThread currentThread].name ;
3.GCD
(1)dispatch_async创建一个线程
// dispatch_async(dispatch_get_global_queue(0, 0), ^{
// //执行耗时任务
// NSLog(@"start task 1");
// [NSThread sleepForTimeInterval:2];
// NSLog(@"end task 1");
// dispatch_async(dispatch_get_main_queue(), ^{
// //刷新UI
// NSLog(@"回到主线程,刷新UI");
// });
// });
(2)多线程的线程的优先级别
//dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
// //执行耗时任务
// NSLog(@"start task 1");
// [NSThread sleepForTimeInterval:2];
// NSLog(@"end task 1");
// });
// dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
// NSLog(@"start task 2");
// [NSThread sleepForTimeInterval:2];
// NSLog(@"end task 2");
// });
// dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// //执行耗时任务
// NSLog(@"start task 3");
// [NSThread sleepForTimeInterval:2];
// NSLog(@"end task 3");
// });

DISPATCH_QUEUE_PRIORITY_HIGH
DISPATCH_QUEUE_PRIORITY_LOW
DISPATCH_QUEUE_PRIORITY_DEFAULT
是优先级别的参数。
(3)dispatch_queue_t 创建异步执行和串行执行queue
dispatch_queue_t queue = dispatch_queue_create("com.text.cn", DISPATCH_QUEUE_CONCURRENT);
// //NULL 为串行的 其实在同一个线程里执行的= DISPATCH_QUEUE_SERIAL
// //DISPATCH_QUEUE_CONCURRENT - 为异步执行
// dispatch_async(queue, ^{
// //执行耗时任务
// NSLog(@"start task 1");
// [NSThread sleepForTimeInterval:2];
// NSLog(@"end task 1");
// });
// dispatch_async(queue, ^{
// //执行耗时任务
// NSLog(@"start task 2");
// [NSThread sleepForTimeInterval:2];
// NSLog(@"end task 2");
// });
// dispatch_async(queue, ^{
// //执行耗时任务
// NSLog(@"start task 3");
// [NSThread sleepForTimeInterval:2];
// NSLog(@"end task 3");
// });
//NULL 为串行的 其实在同一个线程里执行的= DISPATCH_QUEUE_SERIAL
// //DISPATCH_QUEUE_CONCURRENT - 为异步执行
(4)dispatch_group_t 执行一定的任务,可以用dispatch_group_notify接收所有的任务执行完的一个后续操作,当前现在在最后一个执行完的线程里操作,需要回到主线程来操作
dispatch_queue_t queue1 = dispatch_queue_create("com.cn.ctvit.reque", DISPATCH_QUEUE_CONCURRENT); //异步
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue1, ^{
//内部可以执行异步操作,当前不是异步操作,如果是异步操作试一试
NSLog(@"start task 1");
[NSThread sleepForTimeInterval:2];
NSLog(@"end task 1");
});

dispatch_group_async(group, queue1, ^{
    NSLog(@"start task 2");
    [NSThread sleepForTimeInterval:2];
    NSLog(@"end task 2");
});

dispatch_group_async(group, queue1, ^{
    NSLog(@"start task 3");
    [NSThread sleepForTimeInterval:2];
    NSLog(@"end task 3");
});

dispatch_group_notify(group, queue1, ^{
    NSLog(@"all task done");
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"回到主线程");
    });
});

(5)GCD实现单例
(6)GCD实现延迟操作
//延迟执行
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (2*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"dely excute");
});

4.NSInvocationOperation,NSBlockOperation,NSOperation和NSOperationqueue 的组合运用
(1)NSInvocationOperation
//同步执行-放主线程里则会阻塞主线程,放在子线程则会阻塞子线程
// NSInvocationOperation *invocation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(goInvocation) object:nil];
// [invocation start];

//创建一个子线程-则阻塞子线程
// dispatch_async(dispatch_get_global_queue(0, 0), ^{
// NSInvocationOperation *invocation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(goInvocation) object:nil];
// [invocation start];
// });

(2)NSBlockOperation
//同步执行
// NSBlockOperation *blockOpration = [NSBlockOperation blockOperationWithBlock:^{
// for (int i = 0; i < 3; i ++) {
// NSLog(@"start task");
// [NSThread sleepForTimeInterval:2];
// }
// }];
// [blockOpration start];
(3)NSOperation 和 NSOperationQueue
if (!self.oprQueue) {
self.oprQueue = [[NSOperationQueue alloc] init];
}
[self.oprQueue setMaxConcurrentOperationCount:4];

CustomOperation *operationA = [[CustomOperation alloc] initWithName:@"operationA"];
CustomOperation *operationB = [[CustomOperation alloc] initWithName:@"operationB"];
CustomOperation *operationC = [[CustomOperation alloc] initWithName:@"operationC"];
CustomOperation *operationD = [[CustomOperation alloc] initWithName:@"operationD"];

[operationD addDependency:operationA];
[operationA addDependency:operationB];
[operationB addDependency:operationC];  //依赖关系不能相互依赖,也就是循环依赖

//异步执行
[self.oprQueue addOperation:operationA];
[self.oprQueue addOperation:operationB];
[self.oprQueue addOperation:operationC];
[self.oprQueue addOperation:operationD];

//CustomOperation是继承NSOperation 重写了main函数执行的。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 目录: (一)线程与进程之间的区别 (二)为什么需要学习多线程 (三)多线程任务执行方式 (四)多线程执行的...
    KingLionsFrank阅读 792评论 6 6
  • 一、多线程基础 基本概念 进程进程是指在系统中正在运行的一个应用程序每个进程之间是独立的,每个进程均运行在其专用且...
    AlanGe阅读 557评论 0 0
  • 上文我们简单的叙述了多线程,那么这篇我们就详细的说一下! 多线程技术方案 PThread 导入头文件 #impor...
    Clark_new阅读 401评论 5 2
  • 在这篇文章中,我将为你整理一下 iOS 开发中几种多线程方案,以及其使用方法和注意事项。当然也会给出几种多线程的案...
    张战威ican阅读 614评论 0 0
  • 文/行香子闫 我妹妹特别爱吃肉,从小到大,每次吃饭,她总是吃着自己碗里的,眼睛看着我的碗里的!“姐姐,你的吃的完吗...
    行香子闫阅读 368评论 2 3