17.多线程

课程来自慕课网不死鸟fj老师


pThread
- (void)clickPThread {
    NSLog(@"主线程");
    pthread_t pthread;
    pthread_create(&pthread, NULL, run, NULL);
    
}

void *run(void *data) {
    NSLog(@"子线程");
    for (int i = 1; i < 10; i++) {
        NSLog(@"%d",i);
        sleep(1);
    }
    return NULL;
}
NSThread的创建和执行
  1. 通过 alloc init 方式创建并执行线程
    NSThread *thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(runThread1) object:nil];
    [thread1 setName:@"Name_Thread1"];  // 设置线程名称
    [thread1 setThreadPriority:0.2];  // 设置线程优先级 0-1
    [thread1 start];
  1. 通过 detachNewThreadSelector 方式创建并执行线程
    [NSThread detachNewThreadSelector:@selector(runThread1) toTarget:self withObject:nil];
  1. 通过 performSelectorInBackground 方式创建线程
    [self performSelectorInBackground:@selector(runThread1) withObject:nil];
NSThread锁
-(void)sale {
    while (true) {
        @synchronized (self) {
            if (self.tickets > 0) {
                [NSThread sleepForTimeInterval:0.5];
                self.tickets--;
                self.soldCount = Total - self.tickets;
                NSLog(@"%@: 当前余票: %d, 售出: %d",[NSThread currentThread].name,self.tickets,self.soldCount);
            }
        }
    }
}
GCD的创建
    // 获取全局并发队列
    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]);      // 打印当前线程
        });
    });
// 串行队列的创建方法
dispatch_queue_t queue = dispatch_queue_create("com.test.gcd.queue", DISPATCH_QUEUE_SERIAL);
// 并发队列的创建方法
dispatch_queue_t queue = dispatch_queue_create("com.test.gcd.queue", DISPATCH_QUEUE_CONCURRENT);
GCD_Group
    NSLog(@"GCD开始执行");
    dispatch_queue_t queue = dispatch_queue_create("com.test.gcd.group", DISPATCH_QUEUE_CONCURRENT);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);
    [self sendRequest1:^{
        NSLog(@"request1 done");
        dispatch_group_leave(group);
    }]
    [self sendRequest2:^{
        NSLog(@"request2 done");
        dispatch_group_leave(group);
    }]
//    dispatch_group_async(group, queue, ^{
//        NSLog(@"start task 1");
//        [NSThread sleepForTimeInterval:2];
//        NSLog(@"end task 1");
//    });
//    dispatch_group_async(group, queue, ^{
//        NSLog(@"start task 2");
//        [NSThread sleepForTimeInterval:2];
//        NSLog(@"end task 2");
//    });
    dispatch_group_notify(group, queue, ^{
        NSLog(@"All tasks over");
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"回到主线程刷新UI");
        });
    });
NSOperation

NSOperation是个抽象类,并不具备封装操作的能力,必须使⽤它的子类
使用NSOperation⼦类的方式有3种:
(1)NSInvocationOperation
(2)NSBlockOperation
(3)自定义子类继承NSOperation,实现内部相应的⽅法

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

推荐阅读更多精彩内容

  • iOS多线程编程 基本知识 1. 进程(process) 进程是指在系统中正在运行的一个应用程序,就是一段程序的执...
    陵无山阅读 6,155评论 1 14
  • NSThread 第一种:通过NSThread的对象方法 NSThread *thread = [[NSThrea...
    攻城狮GG阅读 862评论 0 3
  • 多线程基本概念 单核CPU,同一时间cpu只能处理1个线程,只有1个线程在执行 。多线程同时执行:是CPU快速的在...
    WeiHing阅读 729评论 1 5
  • 主队列 细心的同学就会发现,每套多线程方案都会有一个主线程(当然啦,说的是iOS中,像 pthread 这种多系统...
    京北磊哥阅读 391评论 0 1
  • 一、认识酵素 1. 何谓酵素 2. 酵素起源 3. 科学家们对酵素的证言 4. 被延误了50年的神奇酵素 5. 酵...
    欣儿麻阅读 287评论 0 0