iOS NSThread

作者: iOS_成才录 | 来源:发表于2015-11-12 19:26 被阅读474次

1、知识梳理

  • 一个NSThread对象就代表一条线程
//  获得当前线程
NSThread*current = [NSThreadcurrentThread];

//  线程的名字
-(void)setName:(NSString*)n;
-(NSString*)name;

创建和启动线程

  • 该方式,需要手动开启线程
NSThread*thread = [[NSThre adalloc] initWithTarget:self selector:@selector(run) object:nil];
[thread start];
//线程一启动,就会在线程thread中执行self的run方法
  • 其他创建线程方式:会自动开启线程
    • 优点:简单快捷
    • 缺点:无法对线程进行更详细的设置:如,设置线程名称等
// 创建线程后自动启动线程
[NSThreaddetachNewThreadSelector:@selector(run)toTarget:selfwithObject:nil];

// 隐式创建并启动线程
[selfperformSelectorInBackground:@selector(run)withObject:nil];

主线程相关用法

+(NSThread*)mainThread;// 获得主线程
-(BOOL)isMainThread;// 是否为主线程
+(BOOL)isMainThread;// 是否为主线程

线程状态

  • 注意:一旦线程停止(死亡)了,就不能再次开启任务

    Snip20151030_17.png
  • 控制线程状态方法:

// 启动线程
-(void)start;
//进入就绪状态->运行状态。当线程任务执行完毕,自动进入死亡状态

 // 阻塞(暂停)线程
+(void)sleepUntilDate:(NSDate*)date;
+(void)sleepForTimeInterval:(NSTimeInterval)ti;
//进入阻塞状态

// 强制停止线程
+(void)exit;
//进入死亡状态

多线程的安全隐患 :

  • 资源共享

    • 1块资源可能会被多个线程共享,也就是多个线程可能会访问同一块资源
    • 比如多个线程访问同一个对象、同一个变量、同一个文件
  • 当多个线程访问同一块资源时,很容易引发数据错乱和数据安全问题

安全隐患解决– 互斥锁

  • 互斥锁使用格式

// 注意:锁定1份代码只用1把锁,用多把锁是无效的
@synchronized(锁对象){ //需要锁定的代码 }

+ 互斥锁的优缺点
- 优点:能有效防止因多线程抢夺资源造成的数据安全问题
- 缺点:需要消耗大量的CPU资源

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

+ 线程同步: 多条线程在同一条线上执行(按顺序地执行任务)
 - 互斥锁,就是使用了线程同步技术

> 线程间通信

+ 线程间通信的体现
 - 1个线程传递数据给另1个线程
 -  在1个线程中执行完特定任务后,转到另1个线程继续执行任务

+ 线程间通信常用方法

```objc
-(void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait;
-(void)performSelector:(SEL)aSelectoron Thread:(NSThread*)thread withObject:(id)arg waitUntilDone:(BOOL)wait;

2、应用

1、买票:互斥锁,解决多线程抢夺同一资源问题

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) NSThread *thread1; /**< 售票员1 */
@property (nonatomic, strong) NSThread *thread2; /**< 售票员2 */
@property (nonatomic, strong) NSThread *thread3; /**< 售票员2 */

@property (nonatomic, assign)NSUInteger totalCount; /**< 票的总数 */
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 0.初始化票数
    self.totalCount = 100;
    // 1.创建3个售票员
    NSThread *thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
    thread1.name = @"售票员1";
    self.thread1 = thread1;
    NSThread *thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
    thread2.name = @"售票员2";
    self.thread2 = thread2;
    NSThread *thread3 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
    thread3.name = @"售票员3";
    self.thread3 = thread3;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 2.让3个售票员同事售票
    [self.thread1 start];
    [self.thread2 start];
    [self.thread3 start];
}

// 售票方法
- (void)saleTicket
{
    while (1) {
        
        NSLog(@"欢迎光临");
        // 只要被synchronized扩住的代码, 就是被锁住的代码 \
        也就是说, 只要被synchronized{}扩住, 就能实现同一时刻, 只能有一个线程操作
        
        /*
        // 注意:
        // 1. 如果多条线程访问同一个资源, 那么必须使用同一把锁才能锁住
        // 2. 在开发中, 尽量不要加锁, 如果必须要加锁, 一定记住, 锁的范围不能太大, 哪里会有安全隐患就加在哪里
         */
//         NSObject lockObj = [[NSObject alloc] init];
        /*
        技巧: 
         1.@synchronized单词的快速记忆方法  [NSUserDefaults standardUserDefaults] synchronize + d
         2.开发中如果需要加锁, 一般都使用self
         */
       
        // 线程2: 等待,  线程3: 等待
        @synchronized(self){ // 锁住
        
            // 1.查询剩余的票数
            NSUInteger count = self.totalCount;
            // 2.判断是否还有余票
            if (count > 0) {
                // 线程1 100
                [NSThread sleepForTimeInterval:0.1];
                // 2.1卖票
                self.totalCount = count - 1; // 99
                NSLog(@"%@卖了一张票, 还剩%zd票", [NSThread currentThread].name, self.totalCount);
            }else
            {
                // 3.提示客户, 没有票了
                NSLog(@"对不起, 没有票了");
                break;
            }
        } // 解锁
    
    }   
}
@end
  1. 线程间通信
  • 实例:在子线程中下载图片,回到主线程刷新界面
  • 代码实现:如下
#import "ViewController.h"
@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end

@implementation ViewController

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"%s", __func__);
    
    // 开启一个子线程下载图片
    [self performSelectorInBackground:@selector(downlod) withObject:nil];
}

- (void)downlod
{
    NSLog(@"%@", [NSThread currentThread]);
    // 1.下载图片
     NSURL *url = [NSURL URLWithString:@"https://www.baidu.com/img/bd_logo1.png"];
     NSData *data = [NSData dataWithContentsOfURL:url];
    // 2.将二进制转换为图片
    UIImage *image = [UIImage imageWithData:data];
    
    // 3.跟新UI
#warning 注意: 千万不要在子线程中更新UI, 会出问题
//    self.imageView.image = image;
    
    /*
     waitUntilDone: 
     YES: 如果传入YES, 那么会等待updateImage方法执行完毕, 才会继续执行后面的代码
     NO:  如果传入NO, 那么不会等待updateImage方法执行完毕, 就可以继续之后后面的代码
     */
    /*
    [self performSelectorOnMainThread:@selector(updateImage:) withObject:image waitUntilDone:NO];
    
    NSLog(@"------------");
     */
    
    // 开发中常用
//    [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];
    
    // 可以在指定的线程中, 调用指定对象的指定方法
    [self performSelector:@selector(updateImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:YES];
    
}

- (void)updateImage:(UIImage *)image
{
    NSLog(@"%@", [NSThread currentThread]);
    // 3.更新UI
    self.imageView.image = image;
}
@end

相关文章

网友评论

本文标题:iOS NSThread

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