美文网首页
创建线程

创建线程

作者: 闲得一B | 来源:发表于2016-05-03 17:20 被阅读74次

方法一:NSThread

    一:[[NSThread alloc]init];直接创建
    二:[[NSThread alloc] initWithTarget:调用谁的 selector:方法 object:参数]。需要手动调用start方法
    三:[NSThread detachNewThreadSelector:方法 toTarget:调用谁的 withObject:参数];从主线程中分离一条线程出来。不需要手动调用start方法。
    四:[self performSelectorInBackground:方法 withObject:参数];在后台(子线程)调用这个方法。没有start方法。
需要手动调用start方法的原因是:
默认创建一个线程是`新建状态New`,当调用start方法是会将线程放到`可调度线程池`中,此时为`就绪状态Runnable`,只有在可调用线程池中的线程才会被调用。
当cpu调用时线程,此时线程为`运行状态Running`。当调用线程的sleep时,进入`阻塞状态Blocked`。当线程执行完毕或者意外死亡,此时线程为`死亡状态Dead`。

方法二:NSOperation之NSInvocationOperation

//将NSInvocationOperation操作添加到队列中
NSInvocationOperation *invocationOperation =     [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(方法) object:nil];
NSOperationQueue *operationQueue =    [[NSOperationQueue alloc]init];
[operationQueue addOperation:invocationOperation];

方法三:NSOperation之NSBlockOperation
NSBlockOperation有两种方式
第一种:将操作放到队列中

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

第二种:只要NSBlockOperation封装的操作数大于1就会创建线程,不需要我们手动将操作放入到队列中

NSBlockOperation *blockOperation = [NSBlockOperation 
//这个操作在主线程中执行
blockOperationWithBlock:^{
        NSLog(@"%@",[NSThread currentThread]);
    }];
//这个线程在子线程中执行
[blockOperation addExecutionBlock:^{
        NSLog(@"%@",[NSThread currentThread]);
    }];
[blockOperation start];

注意:NSOperation的NSInvocationOperation和NSBlockOperation如果不加入队列都需要手动开启start。已经加入到队列中的操作如果再次手动start就会报错。

错误信息如下: 
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSOperationInternal _start:]: something other than the operation queue it is in is trying to start the receiver'

方法四:自定义NSOperation重写main方法

自定义一个类,继承自NSOperation,重写main方法。
创建这个对象,将这个对象加入到队列中NSOperationQueue,就能在子线程中执行。

方法五:GCD只有异步才具备开启线程的能力
第一种:异步+手动创建串行队列:都在一个线程中执行

//串行队列
dispatch_queue_t  queue =  dispatch_queue_create(nil, DISPATCH_QUEUE_SERIAL);
dispatch_async(queue1, ^{
        for (int a = 0; a < 100;a++) {
            NSLog(@"1111111=========%@",[NSThread currentThread]);
        }  
 }) ;
dispatch_async(queue1, ^{
        for (int a = 0; a < 100;a++) {
            NSLog(@"222222=========%@",[NSThread currentThread]);
        }  
 }) ;
dispatch_async(queue1, ^{
        for (int a = 0; a < 100;a++) {
            NSLog(@"333333=========%@",[NSThread currentThread]);
        }  
 }) ;

第二种:异步+手动创建并发队列:在不同线程中执行

dispatch_queue_t queue1 =     dispatch_queue_create(nil, DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue1, ^{
        for (int a = 0; a < 100;a++) {
            NSLog(@"1111111=========%@",[NSThread currentThread]);
        }
    }) ;
    dispatch_async(queue1, ^{
        for (int a = 0; a < 100;a++) {
            NSLog(@"2222222=========%@",[NSThread currentThread]);
        }
    }) ;
    dispatch_async(queue1, ^{
        for (int a = 0; a < 100;a++) {
            NSLog(@"3333333=========%@",[NSThread currentThread]);
        }
    }) ;

相关文章

  • Java 多线程之线程的创建及其使用

    一、创建线程以及启动线程 二、停止线程 三、线程类中函数详解 一、创建线程以及启动线程 创建线程:Java中创建线...

  • 多线程

    创建一个多线程 创建多线程-继承线程类 创建多线程-实现Runnable接口 创建多线程-匿名类code

  • iOS 多线程-NSThread

    1. 创建和启动线程 创建、启动线程 2. 其他创建线程方式 创建线程后自动启动线程[NSThread detac...

  • Day19——threading

    一、导入线程库 二、创建子线程 函数创建线程 Thread(target,args) - 创建子线程对象说明...

  • 创建线程的几种方法

    直接创建 创建线程后自动启动 创建后台线程

  • 多线程编程之NSThread

    NSThread创建线程很简单,管理线程很困难 一、创建线程 1、创建线程对象,调用start方法执行线程 NST...

  • 多线程--精通ThreadPoolExecutor

    前言 在多线程开发中,应该避免显式创建线程,而是采用线程池里面的线程。使用线程池可以减少手动创建线程,减少线程创建...

  • Java 必备面试代码

    1. Thread 创建线程 2. Runnable 创建线程 使用线程池创建多线程Callable 4. 基于线...

  • 3.多线程基础(三)NSThead使用

    1.NSThread创建新线程: 创建线程之后是默认不执行的状态 创建线程设置线程的属性 2.NSThread创建...

  • java 创建线程的几种方式

    继承Thread类创建线程类 通过Runable接口创建线程类 通过Callable和FutureTask创建线程...

网友评论

      本文标题:创建线程

      本文链接:https://www.haomeiwen.com/subject/nafrrttx.html