ios开发网络第一天之04线程的状态

#pragma mark - 触摸方法

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    

    //1.创建一个线程对象(在内存中开辟空间存储对象)

    NSThread *thread = [[NSThread alloc]initWithTarget:

                        self selector:@selector(longTimeOperatinon:) object:@"hahaha"];

    //2.启动线程(就绪状态 - 将当前线程放在可调用线程池中,等待CPU调度);

    [thread start];

    

}


#pragma mark - 耗时操作

- (void)longTimeOperatinon:(id) object{

  

    

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

        if (i == 30) {

        

            //3.让线程睡眠(阻塞:将线程对象从可调度线程池中移除,但还是在内存中)

            //阻塞到指定事件结束阻塞

//            NSDateFormatter *formatter = [[NSDateFormatter alloc]init];

//            [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];

//            NSDate *date1 = [formatter dateFromString:@"2016-4-24 03:22:00"];

//            NSLog(@"%@::%@",date1,[NSDate date]);

//            [NSThread sleepUntilDate:date1];

            //阻塞指定的时间后结束阻塞

            [NSThread sleepForTimeInterval:10];

         

            

        }

        

        if (i == 99) {

            //强制退出(死亡状态 - 将线程从可调度线程池中移除然后销毁)

            

            [NSThread exit];

        

        }

        

        NSLog(@"%@%@%d",[NSThread currentThread],object,i);

    }

    

    

}


你可能感兴趣的:(线程的状态)