网络开始---多线程---NSThread-02-线程状态(了解)(三)

 1 #import "HMViewController.h"
 2 
 3 @interface HMViewController ()
 4 @property (nonatomic, strong) NSThread *thread;
 5 @end
 6 
 7 @implementation HMViewController
 8 
 9 - (void)viewDidLoad
10 {
11     [super viewDidLoad];
12     
13     self.thread = [[NSThread alloc] initWithTarget:self selector:@selector(download) object:nil];
14     // Do any additional setup after loading the view, typically from a nib.
15 }
16 
17 - (void)download
18 {
19     NSLog(@"-----begin");
20     
21     // 睡眠5秒钟 睡眠是用来阻塞线程的,这样一写,线程会睡眠5秒以后再执行下面的语句
22 //    [NSThread sleepForTimeInterval:5];
23     
24     // 3秒后的时间  从现在开始睡到3秒后 也就是3秒后开始执行
25 //    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:3];
26 //    [NSThread sleepUntilDate:date];
27     
28     for (int i = 0; i<100; i++) {
29         NSLog(@"------%d", i);
30 //        return;  直接写一个return可以直接强制停止线程 让线程停止执行
31         
32 //        if (i == 49) {
33 //            [NSThread exit]; 这个也是强制停止线程
34 //        }
35     }
36     
37     
38     NSLog(@"-----end");
39 }
40 
41 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
42 {
43 //    [self.thread start];
44     
45     //创建线程
46     NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(download) object:nil];
47     
48     //启动线程
49     [thread start];
50     
51     //线程执行完毕就会进入死亡状态,不用我们管,这里线程执行完end线程就没了
52     
53     //一旦线程停止(死亡了),就不能再次开启线程
54     
55     //一条线程只能使用一次
56 }
57 
58 @end

 

posted @ 2015-09-18 00:57  红街咖啡  阅读(168)  评论(0编辑  收藏  举报