zoukankan      html  css  js  c++  java
  • IOS 多线程-NSThread 和线程状态

    @interface HMViewController ()
    - (IBAction)btnClick;
    
    @end
    
    @implementation HMViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    
    - (IBAction)btnClick {
        // 1.获得当前的线程
        NSThread *current = [NSThread currentThread];
        NSLog(@"btnClick---%@", current);
        
    //    NSThread *main = [NSThread mainThread];
    //    NSLog(@"btnClick---%@", main);
        
        // 2.执行一些耗时操作 : 创建一条子线程
        [self threadCreate];
    }
    
    - (void)run:(NSString *)param
    {
        NSThread *current = [NSThread currentThread];
        
        for (int i = 0; i<10000; i++) {
            NSLog(@"%@----run---%@", current, param);
        }
    }
    
    /**
     * NSThread的创建方式
     * 隐式创建线程, 并且直接(自动)启动
     */
    - (void)threadCreate3
    {
        // 在后台线程中执行 === 在子线程中执行
        [self performSelectorInBackground:@selector(run:) withObject:@"abc参数"];
    }
    
    /**
     * NSThread的创建方式
     * 创建完线程直接(自动)启动
     */
    - (void)threadCreate2
    {
        [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"我是参数"];
    }
    
    /**
     * NSThread的创建方式
     * 1> 先创建初始化线程
     * 2> start开启线程
     */
    - (void)threadCreate
    {
        NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"哈哈"];
        thread.name = @"线程A";
        // 开启线程
        [thread start];
        
        NSThread *thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"哈哈"];
        thread2.name = @"线程B";
        // 开启线程
        [thread2 start];
    }
    @end

     线程的状态

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        self.thread = [[NSThread alloc] initWithTarget:self selector:@selector(test) object:nil];
        self.thread.name = @"线程A";
    }
    
    - (void)test
    {
        NSLog(@"test - 开始 - %@", [NSThread currentThread].name);
        
    //    [NSThread sleepForTimeInterval:5]; // 阻塞状态
        
    //    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:5.0];
    //    [NSThread sleepUntilDate:date];
        
        for (int i = 0; i<1000; i++) {
            NSLog(@"test - %d - %@", i, [NSThread currentThread].name);
            
            if (i == 50) {
                [NSThread exit];
            }
        }
        
        NSLog(@"test - 结束 - %@", [NSThread currentThread].name);
    }
  • 相关阅读:
    Linux-C基础知识学习:C语言作业-将5个学生成绩保存在一个数组中,单独实现一个计算平均成绩的average函数, 在main函数中获取该函数返回的平均值,并打印。
    Linux-C基础知识学习:C语言作业-输入两个数,将两个数交换,按升序输出。
    C语言学习:结构体(笔记)--未完待续
    C语言学习:结构体(笔记)
    PHP之函数
    PHP之流程控制
    PHP之常量和变量
    PHP之数据类型
    PHP之标记风格和注释
    VMware虚拟机中各类文件作用详解
  • 原文地址:https://www.cnblogs.com/liuwj/p/6601940.html
Copyright © 2011-2022 走看看