iOS开发——多线程、NSThread、gcd、线程间通信

来源:互联网 发布:电动汽车专用单片机 编辑:程序博客网 时间:2024/05/16 19:40

1.基本概念

  • 1.1 进程

    进程是指在系统中正在运行的一个应用程序。每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内。

  • 1.2 线程

(1)基本概念

1个进程要想执行任务,必须得有线程(每1个进程至少要有1条线程),线程是进程的基本执行单元,一个进程(程序)的所有任务都在线程中执行。

(2)线程的串行

 1个线程中任务的执行是串行的,如果要在1个线程中执行多个任务,那么只能一个一个地按顺序执行这些任务。也就是说,在同一时间内,1个线程只能执行1个任务。
  • 1.3 多线程

(1)基本概念

即1个进程中可以开启多条线程,每条线程可以并行(同时)执行不同的任务。

(2)线程的并行

并行即同时执行。比如同时开启3条线程分别下载3个文件(分别是文件A、文件B、文件C。

(3)多线程并发执行的原理

 在同一时间里,CPU只能处理1条线程,只有1条线程在工作(执行)。多线程并发(同时)执行,其实是CPU快速地在多条线程之间调度(切换),如果CPU调度线程的时间足够快,就造成了多线程并发执行的假象

(4)多线程优缺点

优点    1)能适当提高程序的执行效率。    2)能适当提高资源利用率(CPU、内存利用率)缺点    1)开启线程需要占用一定的内存空间(默认情况下,主线程占用1M,子线程占用512KB),如果开启大量的线程,会占用大量的内存空间,降低程序的性能。    2)线程越多,CPU在调度线程上的开销就越大。    3)程序设计更加复杂:比如线程之间的通信、多线程的数据共享
  • 1.4 多线程在iOS开发中的应用

(1)主线程

    1)一个iOS程序运行后,默认会开启1条线程,称为“主线程”或“UI线程”。    2)作用。刷新显示UI,处理UI事件。

(2)使用注意

    1)不要将耗时操作放到主线程中去处理,会卡住线程。
  • 1.5 iOS中多线程的实现方案

(1)pthread

01 特点:(1)一套通用的多线程API(2)适用于Unix\Linux\Windows等系统(3)跨平台\可移植(4)使用难度大02 使用语言:c语言03 使用频率:几乎不用04 线程生命周期:由程序员进行管理

(2) NSThread

01 特点:(1)使用更加面向对象(2)简单易用,可直接操作线程对象02 使用语言:OC语言03 使用频率:偶尔使用04 线程生命周期:由程序员进行管理

(3)GCD

01 特点:(1)旨在替代NSThread等线程技术(2)充分利用设备的多核(自动)02 使用语言:OC语言03 使用频率:经常使用04 线程生命周期:自动管理

(4) NSOperation

01 特点:(1)基于GCD(底层是GCD)(2)比GCD多了一些更简单实用的功能(3)使用更加面向对象02 使用语言:OC语言03 使用频率:经常使用04 线程生命周期:自动管理

2.pthread

(1)pthread的基本使用(需要包含头文件)

    //使用pthread创建线程   pthread_t thread;    NSString *name = @"wendingding";    //使用pthread创建线程    //第一个参数:线程对象地址    //第二个参数:线程属性    //第三个参数:指向函数的执行    //第四个参数:传递给该函数的参数    pthread_create(&thread, NULL, run, (__bridge void *)(name));

3.NSThread

(1)NSThread的基本使用

//第一种创建线程的方式:alloc init.//特点:需要手动开启线程,可以拿到线程对象进行详细设置    //创建线程    /*     第一个参数:目标对象     第二个参数:选择器,线程启动要调用哪个方法     第三个参数:前面方法要接收的参数(最多只能接收一个参数,没有则传nil)     */    NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"wendingding"];     //启动线程    [thread start];//第二种创建线程的方式:分离出一条子线程//特点:自动启动线程,无法对线程进行更详细的设置    /*     第一个参数:线程启动调用的方法     第二个参数:目标对象     第三个参数:传递给调用方法的参数     */    [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"我是分离出来的子线程"];//第三种创建线程的方式:后台线程//特点:自动启动县城,无法进行更详细设置[self performSelectorInBackground:@selector(run:) withObject:@"我是后台线程"];

(2)设置线程的属性

   //设置线程的属性    //设置线程的名称    thread.name = @"线程A";    //设置线程的优先级,注意线程优先级的取值范围为0.0~1.0之间,1.0表示线程的优先级最高,如果不设置该值,那么理想状态下默认为0.5    thread.threadPriority = 1.0;

(3)线程的状态(了解)

//线程的各种状态:新建-就绪-运行-阻塞-死亡//常用的控制线程状态的方法[NSThread exit];//退出当前线程[NSThread sleepForTimeInterval:2.0];//阻塞线程[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:2.0]];//阻塞线程//注意:线程死了不能复生

(4)线程安全

    01 前提:多个线程访问同一块资源会发生数据安全问题    02 解决方案:加互斥锁    03 相关代码:@synchronized(self){}    04 专业术语-线程同步    05 原子和非原子属性(是否对setter方法加锁)

(5)线程间通信

-(void)touchesBegan:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{//    [self download2];    //开启一条子线程来下载图片    [NSThread detachNewThreadSelector:@selector(downloadImage) toTarget:self withObject:nil];}-(void)downloadImage{    //1.确定要下载网络图片的url地址,一个url唯一对应着网络上的一个资源    NSURL *url = [NSURL URLWithString:@"http://p6.qhimg.com/t01d2954e2799c461ab.jpg"];    //2.根据url地址下载图片数据到本地(二进制数据    NSData *data = [NSData dataWithContentsOfURL:url];    //3.把下载到本地的二进制数据转换成图片    UIImage *image = [UIImage imageWithData:data];    //4.回到主线程刷新UI    //4.1 第一种方式//    [self performSelectorOnMainThread:@selector(showImage:) withObject:image waitUntilDone:YES];    //4.2 第二种方式//    [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];    //4.3 第三种方式    [self.imageView performSelector:@selector(setImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:YES];}

(6)如何计算代码段的执行时间

//第一种方法    NSDate *start = [NSDate date];    //2.根据url地址下载图片数据到本地(二进制数据)    NSData *data = [NSData dataWithContentsOfURL:url];    NSDate *end = [NSDate date];    NSLog(@"第二步操作花费的时间为%f",[end timeIntervalSinceDate:start]);//第二种方法    CFTimeInterval start = CFAbsoluteTimeGetCurrent();    NSData *data = [NSData dataWithContentsOfURL:url];    CFTimeInterval end = CFAbsoluteTimeGetCurrent();    NSLog(@"第二步操作花费的时间为%f",end - start);

4.GCD

(1)GCD基本知识

01 两个核心概念-队列和任务02 同步函数和异步函数

(2)GCD基本使用【重点】

01 异步函数+并发队列:开启多条线程,并发执行任务02 异步函数+串行队列:开启一条线程,串行执行任务03 同步函数+并发队列:不开线程,串行执行任务04 同步函数+串行队列:不开线程,串行执行任务05 异步函数+主队列:不开线程,在主线程中串行执行任务06 同步函数+主队列:不开线程,串行执行任务(注意死锁发生)07 注意同步函数和异步函数在执行顺序上面的差异

(3)GCD线程间通信

 //0.获取一个全局的队列    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);    //1.先开启一个线程,把下载图片的操作放在子线程中处理    dispatch_async(queue, ^{       //2.下载图片        NSURL *url = [NSURL URLWithString:@"http://h.hiphotos.baidu.com/zhidao/pic/item/6a63f6246b600c3320b14bb3184c510fd8f9a185.jpg"];        NSData *data = [NSData dataWithContentsOfURL:url];        UIImage *image = [UIImage imageWithData:data];        NSLog(@"下载操作所在的线程--%@",[NSThread currentThread]);        //3.回到主线程刷新UI        dispatch_async(dispatch_get_main_queue(), ^{           self.imageView.image = image;           //打印查看当前线程            NSLog(@"刷新UI---%@",[NSThread currentThread]);        });    });

(4)GCD其它常用函数

    01 栅栏函数(控制任务的执行顺序)    dispatch_barrier_async(queue, ^{        NSLog(@"--dispatch_barrier_async-");    });    02 延迟执行(延迟·控制在哪个线程执行)      dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_global_  queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{        NSLog(@"---%@",[NSThread currentThread]);    });    03 一次性代码(注意不能放到懒加载)    -(void)once    {        //整个程序运行过程中只会执行一次        //onceToken用来记录该部分的代码是否被执行过        static dispatch_once_t onceToken;        dispatch_once(&onceToken, ^{            NSLog(@"-----");        });    }    04 快速迭代(开多个线程并发完成迭代操作)       dispatch_apply(subpaths.count, queue, ^(size_t index) {    });    05 队列组(同栅栏函数)    //创建队列组    dispatch_group_t group = dispatch_group_create();    //队列组中的任务执行完毕之后,执行该函数    dispatch_group_notify(dispatch_group_t group,    dispatch_queue_t queue,    dispatch_block_t block);

2.demo实例

   demo1     gcd基本使用:

     
#import "ViewController.h"//libdispatch@interface ViewController ()@end@implementation ViewController-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    [self syncConcurrent];}//同步函数+主队列:死锁-(void)syncMain{    NSLog(@"----");    //1.获得主队列    dispatch_queue_t queue =  dispatch_get_main_queue();        //2.同步函数    dispatch_sync(queue, ^{        NSLog(@"---download1---%@",[NSThread currentThread]);    });        dispatch_sync(queue, ^{        NSLog(@"---download2---%@",[NSThread currentThread]);    });        dispatch_sync(queue, ^{        NSLog(@"---download3---%@",[NSThread currentThread]);    });    dispatch_sync(queue, ^{        NSLog(@"---download4---%@",[NSThread currentThread]);    });    }//异步函数+主队列:不会开线程,任务串行执行-(void)asyncMain{    //1.获得主队列    dispatch_queue_t queue =  dispatch_get_main_queue();        //2.异步函数    dispatch_async(queue, ^{        NSLog(@"---download1---%@",[NSThread currentThread]);    });        dispatch_async(queue, ^{        NSLog(@"---download2---%@",[NSThread currentThread]);    });        dispatch_async(queue, ^{        NSLog(@"---download3---%@",[NSThread currentThread]);    });    dispatch_async(queue, ^{        NSLog(@"---download4---%@",[NSThread currentThread]);    });}//同步函数+串行队列:不会开线程,任务串行执行-(void)syncSerial{        //创建串行队列    /*     第一个参数:C语言字符串,标签     第二个参数:     DISPATCH_QUEUE_CONCURRENT:并发队列     DISPATCH_QUEUE_SERIAL:串行队列     */    dispatch_queue_t queue =  dispatch_queue_create("hello apple", DISPATCH_QUEUE_SERIAL);        dispatch_sync(queue, ^{        NSLog(@"---download1---%@",[NSThread currentThread]);    });        dispatch_sync(queue, ^{        NSLog(@"---download2---%@",[NSThread currentThread]);    });        dispatch_sync(queue, ^{        NSLog(@"---download3---%@",[NSThread currentThread]);    });    dispatch_sync(queue, ^{        NSLog(@"---download4---%@",[NSThread currentThread]);    });}//同步函数+并发队列:不会开线程,任务串行执行-(void)syncConcurrent{    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);        NSLog(@"--syncConcurrent--start-");        dispatch_sync(queue, ^{        NSLog(@"---download1---%@",[NSThread currentThread]);    });        dispatch_sync(queue, ^{        NSLog(@"---download2---%@",[NSThread currentThread]);    });        dispatch_sync(queue, ^{        NSLog(@"---download3---%@",[NSThread currentThread]);    });    dispatch_sync(queue, ^{        NSLog(@"---download4---%@",[NSThread currentThread]);    });          NSLog(@"--syncConcurrent--end-");}//异步函数+串行队列:会开启一条线程,任务串行执行-(void)asyncSerial{    //创建串行队列    /*     第一个参数:C语言字符串,标签     第二个参数:     DISPATCH_QUEUE_CONCURRENT:并发队列     DISPATCH_QUEUE_SERIAL:串行队列     */    dispatch_queue_t queue =  dispatch_queue_create("hello apple", DISPATCH_QUEUE_SERIAL);        dispatch_async(queue, ^{        NSLog(@"---download1---%@",[NSThread currentThread]);    });        dispatch_async(queue, ^{        NSLog(@"---download2---%@",[NSThread currentThread]);    });        dispatch_async(queue, ^{        NSLog(@"---download3---%@",[NSThread currentThread]);    });    dispatch_async(queue, ^{        NSLog(@"---download4---%@",[NSThread currentThread]);    });}//异步函数+并发队列:会开启新的线程,并发执行-(void)asyncCONCURRENT{    //创建并发队列    /*     第一个参数:C语言字符串,标签     第二个参数:     DISPATCH_QUEUE_CONCURRENT:并发队列     DISPATCH_QUEUE_SERIAL:串行队列     *///    dispatch_queue_t queue =  dispatch_queue_create("com.520it.download", DISPATCH_QUEUE_CONCURRENT);    NSLog(@"--asyncCONCURRENT--start-");    //获得全局并发队列    /*     第一个参数:队列的优先级DISPATCH_QUEUE_PRIORITY_DEFAULT     第二个参数:永远传0     */    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);    //异步函数    /*     第一个参数:队列     第二个参数:block,在里面封装任务     */    dispatch_async(queue, ^{        NSLog(@"---download1---%@",[NSThread currentThread]);    });        dispatch_async(queue, ^{        NSLog(@"---download2---%@",[NSThread currentThread]);    });        dispatch_async(queue, ^{        NSLog(@"---download3---%@",[NSThread currentThread]);    });    dispatch_async(queue, ^{        NSLog(@"---download4---%@",[NSThread currentThread]);    });          NSLog(@"--asyncCONCURRENT--end-");}

      

   demo2   gcd 线程间的通信


      
#import "ViewController.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UIImageView *imageView;@end@implementation ViewController-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    //1.开子线程下载图片    //创建队列(并发)    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);        //异步函数    dispatch_async(queue, ^{                //1.获取url地址        NSURL *url = [NSURL URLWithString:@"http://www.huabian.com/uploadfile/2015/0914/20150914014032274.jpg"];                //2.下载图片        NSData *data = [NSData dataWithContentsOfURL:url];        //3.把二进制数据转换成图片        UIImage *image = [UIImage imageWithData:data];                NSLog(@"----%@",[NSThread currentThread]);                dispatch_sync(dispatch_get_main_queue(), ^{            self.imageView.image = image;            NSLog(@"+++%@",[NSThread currentThread]);        });    });}@end

   demo3   使用GCD的group判断几个异步线程执行完成

#import "ViewController.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UIImageView *imageView;@property (nonatomic, strong) UIImage  *image1; /**< 图片1 */@property (nonatomic, strong) UIImage  *image2; /**< 图片2 */@end@implementation ViewController-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//    [self once];//    [self applay];//    [self moveFile1];    [self group1];}- (void)group1{    /**    有a、b、c、d 4个异步请求,如何判断a、b、c、d都完成执行?如果需要a、b、c、d顺序执行,该如何实现?    对于这四个异步请求,要判断都执行完成最简单的方式就是通过GCD的group来实现:          当然,我们还可以使用非常老套的方法来处理,通过四个变量来标识a、b、c、d四个任务是否完成,然后在runloop中让其等待,当完成时才退出runloop。但是这样做会让后面的代码得不到执行,直到Run loop执行完毕。     解释:要求顺序执行,那么可以将任务放到串行队列中,自然就是按顺序来异步执行了。        */        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);    dispatch_group_t group = dispatch_group_create();    dispatch_group_async(group, queue, ^{ /*任务a */ NSLog(@"执行a");});    dispatch_group_async(group, queue, ^{ /*任务b */ NSLog(@"执行b");});    dispatch_group_async(group, queue, ^{ /*任务c */ NSLog(@"执行c");});    dispatch_group_async(group, queue, ^{ /*任务d */ NSLog(@"执行d");});    dispatch_group_notify(group,dispatch_get_main_queue(), ^{        // 在a、b、c、d异步执行完成后,会回调这里        NSLog(@"执行完成");    });}

   demo4  gcd常用函数

-(void)group{    //下载图片1        //创建队列组    dispatch_group_t group =  dispatch_group_create();        //1.开子线程下载图片    //创建队列(并发)    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);        dispatch_group_async(group, queue, ^{        //1.获取url地址        NSURL *url = [NSURL URLWithString:@"http://www.huabian.com/uploadfile/2015/0914/20150914014032274.jpg"];                //2.下载图片        NSData *data = [NSData dataWithContentsOfURL:url];                //3.把二进制数据转换成图片        self.image1 = [UIImage imageWithData:data];                NSLog(@"1---%@",self.image1);    });      //下载图片2    dispatch_group_async(group, queue, ^{        //1.获取url地址        NSURL *url = [NSURL URLWithString:@"http://img1.3lian.com/img2011/w12/1202/19/d/88.jpg"];                //2.下载图片        NSData *data = [NSData dataWithContentsOfURL:url];                //3.把二进制数据转换成图片        self.image2 = [UIImage imageWithData:data];        NSLog(@"2---%@",self.image2);            });        //合成    dispatch_group_notify(group, queue, ^{                //开启图形上下文        UIGraphicsBeginImageContext(CGSizeMake(200, 200));                //画1        [self.image1 drawInRect:CGRectMake(0, 0, 200, 100)];                //画2        [self.image2 drawInRect:CGRectMake(0, 100, 200, 100)];                //根据图形上下文拿到图片        UIImage *image =  UIGraphicsGetImageFromCurrentImageContext();                //关闭上下文        UIGraphicsEndImageContext();                dispatch_async(dispatch_get_main_queue(), ^{            self.imageView.image = image;            NSLog(@"%@--刷新UI",[NSThread currentThread]);        });    });}-(void)applay{//    for (NSInteger i=0; i<10; i++) {//        NSLog(@"%zd--%@",i,[NSThread currentThread]);//    }        //创建队列(并发队列)    dispatch_queue_t queue = dispatch_queue_create("com.downloadqueue", DISPATCH_QUEUE_CONCURRENT);    /*     第一个参数:迭代的次数     第二个参数:在哪个队列中执行     第三个参数:block要执行的任务     */    dispatch_apply(10, queue, ^(size_t index) {        NSLog(@"%zd--%@",index,[NSThread currentThread]);    });}-(void)moveFile1{    //文件在哪个地方(文件夹)    NSString *form = @"/Users/xxx/Desktop/form";    //要剪切到什么地方    NSString *to = @"/Users/xxx/Desktop/to";        NSFileManager *manager = [NSFileManager defaultManager];//    NSArray *subpaths =  [manager subpathsAtPath:form];//    NSDirectoryEnumerator *enumer = [manager enumeratorAtPath:to];    NSDirectoryEnumerator *enumer = [manager directoryContentsAtPath:form];    //    for (NSDirectoryEnumerator *en in enumer) {//        NSLog(@"%@",en);//    }////    //创建队列(并发队列)//    dispatch_queue_t queue = dispatch_queue_create("com.downloadqueue", DISPATCH_QUEUE_CONCURRENT);//    //    NSInteger count = [subpaths count];//    dispatch_apply(count, queue, ^(size_t index) {//        //        NSString *subpath = subpaths[index];//        //        NSString *fullPath = [form stringByAppendingPathComponent:subpath];//        //        //拼接目标文件全路径//        NSString *fileName = [to stringByAppendingPathComponent:subpath];//        //        //剪切操作//        [manager moveItemAtPath:fullPath toPath:fileName error:nil];//        //        NSLog(@"%@",[NSThread currentThread]);//    });}-(void)moveFile{    //文件在哪个地方(文件夹)    NSString *form = @"/Users/xxx/Desktop/form";    //要剪切到什么地方    NSString *to = @"/Users/xxx/Desktop/to";        NSFileManager *manager = [NSFileManager defaultManager];    NSArray *subpaths =  [manager subpathsAtPath:form];//    NSLog(@"%@",subpaths);        NSInteger count = [subpaths count];        for (NSInteger i = 0; i<count; i++) {        //拼接文件全路径//        NSString *fullPath = [form stringByAppendingString:<#(nonnull NSString *)#>]                NSString *subpath = subpaths[i];                NSString *fullPath = [form stringByAppendingPathComponent:subpath];                //拼接目标文件全路径        NSString *fileName = [to stringByAppendingPathComponent:subpath];                //剪切操作        [manager moveItemAtPath:fullPath toPath:fileName error:nil];                NSLog(@"%@--%@",fullPath,fileName);    }    }-(void)once{    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        NSLog(@"+++++++++");    });}-(void)delay{    NSLog(@"----");    //表名2秒钟之后调用run//    [self performSelector:@selector(run) withObject:nil afterDelay:2.0];    //    [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:YES];        /*     第一个参数:延迟时间     第二个参数:要执行的代码     */    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_global_queue(0, 0), ^{        NSLog(@"---%@",[NSThread currentThread]);    });    }-(void)run{    NSLog(@"++++");}-(void)barrier{    //1.创建队列(并发队列)    dispatch_queue_t queue = dispatch_queue_create("com.downloadqueue", DISPATCH_QUEUE_CONCURRENT);        dispatch_async(queue, ^{                for (NSInteger i = 0; i<10; i++) {            NSLog(@"%zd-download1--%@",i,[NSThread currentThread]);        }    });        dispatch_async(queue, ^{                for (NSInteger i = 0; i<10; i++) {            NSLog(@"%zd-download2--%@",i,[NSThread currentThread]);        }    });        //栅栏函数    dispatch_barrier_async(queue, ^{        NSLog(@"我是一个栅栏函数");    });        dispatch_async(queue, ^{                for (NSInteger i = 0; i<10; i++) {            NSLog(@"%zd-download3--%@",i,[NSThread currentThread]);        }    });        dispatch_async(queue, ^{                for (NSInteger i = 0; i<10; i++) {            NSLog(@"%zd-download4--%@",i,[NSThread currentThread]);        }    });}

  demo5  NSThread线程间通信

    
#import "ViewController.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UIImageView *imgView;@end@implementation ViewController-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//    http://pic1.ooopic.com/uploadfilepic/sheji/2010-01-13/OOOPIC_1982zpwang407_20100113f68118f451f282f4.jpg        [NSThread detachNewThreadSelector:@selector(download2) toTarget:self withObject:nil];}-(void)download2{    //下载图片    //1.获取url地址    NSURL *url = [NSURL URLWithString:@"http://pic1.ooopic.com/uploadfilepic/sheji/2010-01-13/OOOPIC_1982zpwang407_20100113f68118f451f282f4.jpg"];    //2.下载图片    NSDate *start = [NSDate date];    NSData *data = [NSData dataWithContentsOfURL:url];    NSDate *end = [NSDate date];        NSLog(@"+++++%f",[end timeIntervalSinceDate:start]);    //3.把二进制数据转换成图片    UIImage *image = [UIImage imageWithData:data];        NSLog(@"----%@",[NSThread currentThread]);    //4.回到主线程刷新UI    //    waitUntilDone:        [self performSelectorOnMainThread:@selector(showImage:) withObject:image waitUntilDone:YES];    //    [self.imgView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];//    //    [self.imgView performSelector:@selector(setImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:YES];    }-(void)download{    //下载图片    //1.获取url地址    NSURL *url = [NSURL URLWithString:@"http://pic1.ooopic.com/uploadfilepic/sheji/2010-01-13/OOOPIC_1982zpwang407_20100113f68118f451f282f4.jpg"];    //2.下载图片    CFTimeInterval start = CFAbsoluteTimeGetCurrent();        NSData *data = [NSData dataWithContentsOfURL:url];    CFTimeInterval end = CFAbsoluteTimeGetCurrent();        NSLog(@"+++++%f",end - start);    //3.把二进制数据转换成图片    UIImage *image = [UIImage imageWithData:data];        NSLog(@"----%@",[NSThread currentThread]);    //4.回到主线程刷新UI//    waitUntilDone:    [self performSelectorOnMainThread:@selector(showImage:) withObject:image waitUntilDone:YES];//    [self.imgView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];    //    [self.imgView performSelector:@selector(setImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:YES];        }-(void)showImage:(UIImage *)image{    self.imgView.image = image;     NSLog(@"-showImage---%@",[NSThread currentThread]);}@end

   demo6  线程安全

#import "ViewController.h"@interface ViewController ()//售票员a@property (nonatomic, strong) NSThread *thread01;//售票员b@property (nonatomic, strong) NSThread *thread02;//售票员c@property (nonatomic, strong) NSThread *thread03;//总票数@property(nonatomic, assign) NSInteger totalticket;//@property (nonatomic, strong) NSObject *obj;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    //假设有100张票    self.totalticket = 10;//    self.obj = [[NSObject alloc]init];    //创建线程   self.thread01 =  [[NSThread alloc]initWithTarget:self selector:@selector(saleTicket) object:nil];    self.thread01.name = @"售票员a";        self.thread02 =  [[NSThread alloc]initWithTarget:self selector:@selector(saleTicket) object:nil];    self.thread02.name = @"售票员b";    self.thread03 =  [[NSThread alloc]initWithTarget:self selector:@selector(saleTicket) object:nil];    self.thread03.name = @"售票员c";}-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    //启动线程    [self.thread01 start];    [self.thread02 start];    [self.thread03 start];}//售票-(void)saleTicket{    while (1) {               //2.加互斥锁        @synchronized(self) {            [NSThread sleepForTimeInterval:0.03];            //1.先查看余票数量            NSInteger count = self.totalticket;                        if (count >0) {                self.totalticket = count - 1;                NSLog(@"%@卖出去了一张票,还剩下%zd张票",[NSThread currentThread].name,self.totalticket);            }else            {                NSLog(@"%@发现当前票已经买完了--",[NSThread currentThread].name);                break;            }        }        }}@end

打印:

2014-02-24 15:28:05.268 06-线程安全[6381:145355] 售票员a卖出去了一张票,还剩下9张票

2014-02-24 15:28:05.268 06-线程安全[6381:145356] 售票员b卖出去了一张票,还剩下8张票

2014-02-24 15:28:05.269 06-线程安全[6381:145357] 售票员c卖出去了一张票,还剩下7张票

2014-02-24 15:28:05.269 06-线程安全[6381:145355] 售票员a卖出去了一张票,还剩下6张票

2014-02-24 15:28:05.269 06-线程安全[6381:145356] 售票员b卖出去了一张票,还剩下5张票

2014-02-24 15:28:05.269 06-线程安全[6381:145357] 售票员c卖出去了一张票,还剩下4张票

2014-02-24 15:28:05.270 06-线程安全[6381:145355] 售票员a卖出去了一张票,还剩下3张票

2014-02-24 15:28:05.270 06-线程安全[6381:145356] 售票员b卖出去了一张票,还剩下2张票

2014-02-24 15:28:05.270 06-线程安全[6381:145357] 售票员c卖出去了一张票,还剩下1张票

2014-02-24 15:28:05.270 06-线程安全[6381:145355] 售票员a卖出去了一张票,还剩下0张票

2014-02-24 15:28:05.270 06-线程安全[6381:145356] 售票员b发现当前票已经买完了--

2014-02-24 15:28:05.271 06-线程安全[6381:145357] 售票员c发现当前票已经买完了--

2014-02-24 15:28:05.271 06-线程安全[6381:145355] 售票员a发现当前票已经买完了--

    


0 0
原创粉丝点击