00『 基础知识 』iOS开发iOS学习

浅谈iOS多线程

2016-09-07  本文已影响130人  呜啦啦啦拉拉

iOS多线程有四套多线程方案:

接下来我来一个一个介绍他们

Pthreads

在类Unix操作系统(Unix、Linux、Mac OS X等)中,都使用Pthreads作为操作系统的线程。
这套多线程是使用C语言实现的,所以可移植性很高。但是在实际项目中基本上不会用到,这里只做简单的介绍。
首先要包含头文件pthread.h

void *star(void *data)
{
    NSLog(@"%@",[NSThread currentThread]);
    return NULL;
}
- (IBAction)buttonTouch:(id)sender
{
    pthread_t thread;
    pthread_create(&thread, NULL, star, NULL);
}

控制台输出:

2016-09-07 14:37:44.734 GCDDemo[2660:1124576] <NSThread: 0x7fd2024a58a0>{number = 2, name = (null)}

NSThread

NSThread是苹果封装的面向对象处理多线程的方案。但是他却不够智能,需要我们手动的管理线程的生命周期以及同步。所以这套方案我们通常也不常用。
创建线程:

NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(thisThread) object:nil];
[thread start];

或者使用类方法

[NSThread detachNewThreadSelector:@selector(thisThread) toTarget:self withObject:nil];

或者使用NSObject的类方法:

[self performSelector:@selector(thisThread)];

但是NSThread需要使用NSLock来给线程上锁。以及进行其他操作,它不够智能。所以我们只在很少的情况使用它

GCD

GCD全称Grand Central Dispatch。是苹果提出的多核并行运算的解决方案。也就是说他会自动利用多核CPU,它会自动管理线程的生命周期。只需要我们告诉他应该做什么。
在GCD中有两个概念,一个是任务,一个是队列

创建队列

dispatch_queue_t queue = dispatch_get_main_queue();
NSLog(@"%@",queue);

控制台输出

2016-09-08 14:15:25.361 GCDDemo[6984:2309204] <OS_dispatch_queue: com.apple.main-thread[0x10c34e9c0]>

dispatch_queue_t queue = dispatch_queue_create("wodeduilie", NULL);
NSLog(@"%@",queue);

这个函数有两个参数,一个是队列名,第二个参数控制是否为串行或者并行。第二个参数传入DISPATCH_QUEUE_SERIAL或者NULL表示串行。DISPATCH_QUEUE_CONCURRENT表示并行。

创建任务

dispatch_queue_t queue = dispatch_queue_create("wodeduilie", NULL);
dispatch_sync(queue, ^{
      NSLog(@"%@",[NSThread currentThread]);
});
dispatch_queue_t queue = dispatch_queue_create("wodeduilie", NULL);
dispatch_async(queue, ^{
      NSLog(@"%@",[NSThread currentThread]);
});

NSOperation & NSOperationQueue

NSOperation是面向对象的,它是对GCD的封装。NSOperationNSOperationQueue分别对应GCD任务队列

创建任务

NSOperation是抽象类,如果你想使用它需要使用它的子类:

NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(thisThread) object:nil];
[operation start];
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"%@",[NSThread currentThread]);
    }];
[operation start];

NSBlockOperation中还有一个方法addExecutionBlock:来添加多个任务,而且这些任务是并发执行的

控制台输出:

2016-09-08 22:21:26.660 GCDDemo[8112:2598032] <NSThread: 0x7fd151607d20>{number = 1, name = main}

你需要使用start方法或者cancel方法来开始或者取消一个任务。

创建队列

NSOperationQueue中只有两种队列,一个是主队列,一个是自己创建的其他队列:

NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"%@",[NSThread currentThread]);
}];
NSOperationQueue *queue = [NSOperationQueue mainQueue];
[queue addOperation:operation];

控制台输出:

2016-09-08 22:36:33.053 GCDDemo[8279:2611844] <NSThread: 0x7f828b6020c0>{number = 1, name = main}

NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"%@",[NSThread currentThread]);
}];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:operation];

控制台输出:

2016-09-08 22:40:49.459 GCDDemo[8405:2616956] <NSThread: 0x7f8833e2fd30>{number = 2, name = (null)}

我们使用自定义的队列的时候,加入该队列的任务会自动的并发执行。但是有一个参数表示最大并发执行的数量maxConcurrentOperationCount
当我们将这个参数设为1时就是串行队列。
在NSOperationQueue中我们还可以直接使用方法添加任务

- (void)addOperationWithBlock:(void (^)(void))block;

只要我们将任务添加到队列中就会自动执行任务的start方法。

上一篇 下一篇

猜你喜欢

热点阅读