代码改变世界

多线程 NSThread GCD

2014-11-25 23:11  小鹰爱学习  阅读(180)  评论(0编辑  收藏  举报

ios多线程实现种类

NSThread

NSOperationQueue

NSObject
GCD 

***************

1.NSThread

   //线程

   第一种

    NSThread *thread1=[[NSThread alloc] initWithTarget:self selector:@selector(sum) object:nil];

//    

//    //给线程起名字

    thread1.name=@"thread1";

//    //启动线程

    [thread1 start];

    //结束线程

    [thread1 cancel];

   第二种(不需要手动开启的)

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

 

 

2. NSOperation的两个子类

    

    //.

    NSInvocationOperation *inop=[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(invocation) object:nil];

    

 //   [inop start];

    //.

    NSBlockOperation *blop=[NSBlockOperation blockOperationWithBlock:^{

        

        NSLog(@"我是block");

    }];

    

    //创建队列

    NSOperationQueue *queue=[[NSOperationQueue alloc] init];

    

    //设置最大并行数量

    queue.maxConcurrentOperationCount=2;

    

    //添加事件

    [queue addOperation:inop];

   

    [queue addOperation:blop];

    

 

 

3.NSObject

 [self performSelectorInBackground:@selector(sum) withObject:nil];

4.GCD

    //GCD (先进先出 FIFO)

    //串行:前一个任务完成,后一个任务才能执行

    //并行:任务在派发时有序的,但是不应等第一个任务执行完成才开始.

    //GCD队列分3中:主队列,全局队列,自定义队列

    //1.使用主队列实现任务派发(串行),在主线程中

    dispatch_queue_t mainQueue=dispatch_get_main_queue();

    //1.1添加任务

    dispatch_async(mainQueue, ^{

        NSLog(@"第一个任务:当前线程是%@",[NSThread currentThread]);

    });

 //串行

    //2.自定义队列

    /*

    dispatch_queue_t myQueue=dispatch_queue_create("com.lanlan.myqueue", DISPATCH_QUEUE_SERIAL);

    //2.1添加任务

    

    dispatch_async(myQueue, ^{

        

        NSLog(@"第一个任务:当前线程:%@",[NSThread currentThread]);

    });

//并行

    //3.自定义队列

  /*

    dispatch_queue_t myQueue2=dispatch_queue_create("com.lanlan.myqueue", DISPATCH_QUEUE_CONCURRENT);

    

    //3.1添加任务

    dispatch_async(myQueue2, ^{

        NSLog(@"第一个任务:当前线程:%@",[NSThread currentThread]);

    });

    

 //4.全局队列

   /*

    dispatch_queue_t globelQueue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    

    

    //4.1添加任务

    

    dispatch_async(globelQueue, ^{

        NSLog(@"第一个任务:当前线程:%@",[NSThread currentThread]);

    });

    

5.只保证执行一次

   //保证只执行一次

    

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        NSLog(@"这里的代码只执行一次");

    });

    

6.延迟5秒后执行

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        NSLog(@"等我5秒...");

    });

7.//重复执行

    dispatch_apply(5, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t t) {

        NSLog(@"今天天气不错");

    });

 //子线程

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

 //回到主线程

    [self performSelectorOnMainThread:@selector(mymainTherad) withObject:self waitUntilDone:NO];

-(void)mymainTherad

{

//打印线程

   NSLog(@"我在主线程里%@",[NSThread currentThread]);

}