iOS开发: 多线程

来源:互联网 发布:淘宝助手怎么上架宝贝 编辑:程序博客网 时间:2024/06/08 05:44

iOS开发: 多线程

一、多线程概述:

程序:有源代码生成的可执行应用:QQ.app;

进程:一个正在运行的程序就是一个进程,进程拥有独立运行所需的全部资源:正在运行的QQ就是一个进程;

线程:程序中独立运行的代码段:接收QQ消息的代码;

1、一个进程是由一个或多个线程组成,进程只负责资源的调度和分配,线程才是程序真正的执行单元,负责代码的执行。

2、主线程负责执行程序的所有代码:UI展现、添加以及刷新,网络请求,本地存储等,这些代码只能顺序执行,无法并行执行。

二、多线程实现种类:

(1).NSThread: 优点:代码简单、执行方便

缺点:性能低,效率低

两种方式:

 1。//开辟线程:

    NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(threadAction) object:@"thread"];

   //启动线程:需要手动开启线程

    [thread start];

    2。自动开启线程

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


(2).NSOperation是一个抽象类,需要用子类去实现多线程的任务

//第一个子类

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


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

        //NSOperation的另外一个子类:

    NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{

        

         NSLog(@"当前线程:%@, 是否是主线程:%d", [NSThread currentThread], [NSThread isMainThread]);

    }];

        //创建operation队列

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

        //设置队列元素上限

    [queue setMaxConcurrentOperationCount:3];

        //限制某个任务在某个任务之后执行

//    [operation2 addDependency:operation1];

    //将任务添加进队列

    [queue addOperation:operation1];

    [queue addOperation:operation2];

    [queue addOperation:blockOperation];


}

- (void)downLoadImage{

    

    NSURL *url = [NSURL URLWithString:@"http://img4.duitang.com/uploads/item/201207/28/20120728105310_jvAjW.thumb.600_0.jpeg"];

    

   UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];


    //让图片的加载与缓存在支路里执行,只让赋值在主线程里进行就行

    [self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];

}


- (void)updateUI:(UIImage *)image{

        _imageView.image = image;

}

(3)GCD:Grand Central Dispatch

1。//系统的串行队列

  dispatch_queue_t mainQueue = dispatch_get_main_queue();

//执行线程任务

    dispatch_async(mainQueue, ^{

             NSLog(@"1当前线程为:%@, 是否是主线程:%d", [NSThread currentThread], [NSThread isMainThread]);

          });

  //自定义串行队列

        //参数:1:自定义串行队列的名字;2:系统预留参数

   dispatch_queue_t myQueue = dispatch_queue_create("com.Kevin.myQueue", 0);

     //执行队列任务

   dispatch_async(myQueue, ^{

       NSLog(@"1当前线程为:%@, 是否是主线程:%d", [NSThread currentThread], [NSThread isMainThread]);

       

  });


2。 //系统的并行

    dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

        //执行队列任务

    dispatch_async(globalQueue, ^{

        NSLog(@"1当前线程为:%@, 是否是主线程:%d", [NSThread currentThread], [NSThread isMainThread]);

           });

  //自定义并行

   dispatch_queue_t myGlobalQueue = dispatch_queue_create("com.lanou0710.myGlobalQueue", DISPATCH_QUEUE_CONCURRENT);

   //执行队列任务

    dispatch_async(myGlobalQueue, ^{

       NSLog(@"1当前线程为:%@, 是否是主线程:%d", [NSThread currentThread], [NSThread isMainThread]);

                   });


拓展:

#warning 延迟执行

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

        NSLog(@"此block中的内容会延迟3s执行");

    });

    

    

#warning 只走一次的方法

   static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        NSLog(@"此block中的内容只会走一次");  

    });







    

2 0
原创粉丝点击