线程状态

@interface HMViewController ()
@property (nonatomic, strong) NSThread *thread;
@end

@implementation HMViewController

- (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);
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 开启线程
    [self.thread start];
}

@end

 

posted @ 2015-06-14 10:34  cn.ios  阅读(102)  评论(0)    收藏  举报