iOS开发多线程-线程的状态

一、简单介绍

线程的创建:

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

 说明:创建线程有多种方式,可以参考我 上一篇博客

iOS开发多线程-线程的状态_第1张图片

 

线程的开启:

[self.thread start];

iOS开发多线程-线程的状态_第2张图片

线程的运行和阻塞:


(1)设置线程阻塞1,阻塞2秒

    [NSThread sleepForTimeInterval:2.0];

   

(2)第二种设置线程阻塞2,以当前时间为基准阻塞4秒

    NSDate *date=[NSDate dateWithTimeIntervalSinceNow:4.0];

    [NSThread sleepUntilDate:date];

  iOS开发多线程-线程的状态_第3张图片

  线程处理阻塞状态时在内存中的表现情况:(线程被移出可调度线程池,此时不可调度)


  iOS开发多线程-线程的状态_第4张图片

线程的死亡:


当线程的任务结束,发生异常,或者是强制退出这三种情况会导致线程的死亡。

  iOS开发多线程-线程的状态_第5张图片

线程死亡后,线程对象从内存中移除。

  iOS开发多线程-线程的状态_第6张图片

二、代码示例

代码示例1:

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) NSThread *thread;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 创建线程
    self.thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
}

// 当用户手指触摸屏幕时,开启线程
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    // 开启线程
    [self.thread start];
}

- (void)run
{
   // 获取当前线程
    NSThread *current = [NSThread currentThread];
    NSLog(@"run---打印线程---%@", self.thread.name);
    NSLog(@"run---线程开始---%@", current.name);
    
    // 第一种线程阻塞的设置方式,阻塞2秒钟
    NSLog(@"接下来,线程阻塞2秒钟");
    [NSThread sleepForTimeInterval:2.0];
    
    // 第二种线程阻塞的设置方式,以当前时间为基准阻塞4秒钟
    NSLog(@"接下来,线程阻塞4秒钟");
    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:4.0];
    [NSThread sleepUntilDate:date];
    
    for (NSInteger i = 0; i < 20; i++) {
        NSLog(@"线程---%zd---%@", i, current.name);
    }
    
    NSLog(@"run---线程结束---%@", current.name);
    
}

@end

打印查看:

iOS开发多线程-线程的状态_第7张图片

代码示例2(强制线程退出):

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) NSThread *thread;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 创建线程
    self.thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
}

// 当用户手指触摸屏幕时,开启线程
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    // 开启线程
    // 线程进入就绪 -> 运行状态. 当线程任务执行完毕,自动进入死亡状态
    [self.thread start];
}

- (void)run
{
   // 获取当前线程
    NSThread *current = [NSThread currentThread];
    NSLog(@"run---打印线程---%@", self.thread.name);
    NSLog(@"run---线程开始---%@", current.name);
    
    // 第一种线程阻塞的设置方式,阻塞2秒钟
    NSLog(@"接下来,线程阻塞2秒钟");
    [NSThread sleepForTimeInterval:2.0]; // 线程进入阻塞状态
    
    // 第二种线程阻塞的设置方式,以当前时间为基准阻塞4秒钟
    NSLog(@"接下来,线程阻塞4秒钟");
    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:4.0];
    [NSThread sleepUntilDate:date]; // 线程进入阻塞状态
    
    for (NSInteger i = 0; i < 20; i++) {
        NSLog(@"线程---%zd---%@", i, current.name);
        if (i == 5) {
            // 强制线程退出
            [NSThread exit]; // 线程进入死亡状态
            // 注意: 一旦线程停止(死亡)了,就不能再次开启任务
        }
    }
    
    NSLog(@"run---线程结束---%@", current.name);
    
}

@end


打印查看:

iOS开发多线程-线程的状态_第8张图片

注意:人死不能复生,线程死了也不能复生(重新开启),如果在线程死亡之后,再次点击屏幕尝试重新开启线程,则程序会挂。

iOS开发多线程-线程的状态_第9张图片

你可能感兴趣的:(iOS开发多线程-线程的状态)