美文网首页嵌牛IT观察
多线程----NSThread的使用、数据安全和通信

多线程----NSThread的使用、数据安全和通信

作者: 天使和双彩虹2 | 来源:发表于2017-12-13 22:38 被阅读0次

姓名:谢艾芳    学号:16040410073

转自http://www.jianshu.com/p/d1f15dc6e8c1

〖嵌牛导读〗今天看了一天小码哥的视频,重新理解了一下多线程中的NSThread。希望把自己学到的和理解的能够分享给大家。

〖嵌牛鼻子〗NSThread的使用、数据安全和通信

〖嵌牛提问〗如何快速了解NSThread的使用、数据安全和通信?

〖嵌牛正文〗

NSThread的使用方法

当线程的 number == 1 的时候说明该线程是主线程,反之为子线程

    // 获取主线程

    NSThread * mainThread = [NSThread mainThread];

    NSLog(@"主线程---%zd",mainThread);

   

    // 获取当前线程

    NSThread * currentThread = [NSThread currentThread];

    NSLog(@"子线程---%zd",currentThread);

NSThread的三种创建方法

alloc init  创建线程,需要手动启动线程

    // 1.创建线程

    NSThread * threadA = [[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"ABC"];

   

    // 设置名称

    threadA.name = @"子线程A";

    // 设置线程优先级  取值范围 0.0 ~ 1.0 之间 最高1.0  默认优先级是0.5

    threadA.threadPriority = 0.1;

    // 2.执行线程

    [threadA start];

分离子线程 自动启动线程

[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"分离子线程"];

开启后台线程

[self performSelectorInBackground:@selector(run:) withObject:@"开启后台线程"];

执行方法

- (void) run: (NSString *) param{

    NSLog(@"--------run---------%@---------%@",[NSThread currentThread],param);

}

阻塞线程方法

// 阻塞线程 阻塞时间完成后才会释放线程

    // 方法1.

//    [NSThread sleepForTimeInterval:2.0];

    // 方法2.

    [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:3.0]];

   

    NSLog(@"end-----");

强制退出线程方法

- (void) task{

    for(NSInteger i = 0; i<100; i++){

        NSLog(@"%zd--------%@",i,[NSThread currentThread]);

       

        if(i == 20){

            // 强制退出线程

            [NSThread exit];

            break;

        }

    }

}

生命周期

NSThread的生命周期:当线程执行完成后会自动释放

NSThread的线程安全

互斥锁

@synchronized (<#token#>) {

        <#statements#>

    }

互斥锁优缺点

优点:能有效防止因多线程抢夺资源造成的数据安全问题

缺点:需要消耗大量的CPU资源

使用互斥锁的前提:多线程抢夺同一块资源

相关专业术语:线程同步

线程同步:多条线程在同一条线上执行(按顺序的执行任务)

我们模拟了三个售票员同时出售同100张机票,方法效果如下

设置变量

/** 售票员A */

@property (nonatomic, strong) NSThread * threadA;

/** 售票员B */

@property (nonatomic, strong) NSThread * threadB;

/** 售票员C */

@property (nonatomic, strong) NSThread * threadC;

/** 总票数 */

@property (nonatomic, assign) NSInteger totalCount;

创建并执行线程

    // 设置总票数

    self.totalCount = 100;

   

    // 创建线程

    self.threadA = [[NSThread alloc]initWithTarget:self selector:@selector(saleTicket) object:nil];

    self.threadB = [[NSThread alloc]initWithTarget:self selector:@selector(saleTicket) object:nil];

    self.threadC = [[NSThread alloc]initWithTarget:self selector:@selector(saleTicket) object:nil];

   

    self.threadA.name = @"售票员A";

    self.threadB.name = @"售票员B";

    self.threadC.name = @"售票员C";

   

    // 执行线程

    [self.threadA start];

    [self.threadB start];

    [self.threadC start];

执行方法

如果没有加互斥锁的话会导致同一张票被三个售票员同时售出,这种情况我们当然是不允许的

- (void) saleTicket{

   

    while (1) {

        // 锁:必须是全局唯一的 一般使用 self

        // 1.注意加锁位置

        // 2.注意加锁的前提条件,多线程共享一块资源

        // 3.注意加锁是需要代价的,需要耗费性能和时间

        // 4.加锁的结果:线程同步  当线程A执行的时候进行加锁 线程B在外等着线程A结束 锁开了执行线程B

        @synchronized (self) {

            NSInteger count = self.totalCount;

           

            if(count > 0){

                // 耗时操作

                for(NSInteger i = 0; i<100000; i++){

                   

                }

                self.totalCount = count - 1;

               

                NSLog(@"%@卖出一张票,还剩%zd张票",[NSThread currentThread].name,self.totalCount);

            }else{

                NSLog(@"票已经卖完");

                break;

            }

        }

    }

   

}

NSThread线程间的通信

当子线程任务执行完成之后如何返回值给主线程,再由主线程去刷新UI,下面我们由在网上下载一张图片为栗子

线程间通信的三种方法

/**

    * 参数1. 调用方法

      参数2. 方法传递值

      参数3. 是否完成该方法后执行下一步

    */

1.直接回到主线程

    // 方法1.

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

2.回到指定NSThread线程

    // 方法2.

    [self performSelector:@selector(showImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:YES];

3.省去显示图片函数,直接在方法中实现

    // 方法3.

    [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];

注:方法3.是因为 UIImageView 继承的最底层也是NSObject,而方法

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait;

- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait;

都是继承NSObject,所以可以直接通过 UIImageView 去调用方法,从而设置 UIImageView 的 image --> setImage:

创建线程

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

   

    [thread start];

下载方法

- (void) download{

   

    // 1.获取图片rul

    NSURL * url = [NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1502704595&di=77a802f956215c509727a13dc7176b7a&imgtype=jpg&er=1&src=http%3A%2F%2Fatt.bbs.duowan.com%2Fforum%2F201306%2F08%2F220236m63ppvbxbevgrbrg.jpg"];

   

    // 2.根据url下载图片二进制数据到本地

    NSData * imageData = [NSData dataWithContentsOfURL:url];

   

    // 3.转换图片格式

    UIImage * image = [UIImage imageWithData:imageData];

   

    NSLog(@"download---%@",[NSThread currentThread]);

   

    // 4.回到主线程显示UI

   

    /**

    * 参数1. 调用方法

      参数2. 方法传递值

      参数3. 是否完成该方法后执行下一步

    */

   

    // 方法1.

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

    // 方法2.

//    [self performSelector:@selector(showImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:YES];

    // 方法3.

    [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];

   

    NSLog(@"----end----");

}

显示图片方法

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

    self.imageView.image = image;

    NSLog(@"showImage---%@",[NSThread currentThread]);

相关文章

网友评论

    本文标题:多线程----NSThread的使用、数据安全和通信

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