iOS开发多线程篇—线程的状态
iOS開發多線程篇—線程的狀態
一、簡單介紹
線程的創建:
?self.thread=[[NSThread alloc]initWithTarget:self selector:@selector(test) object:nil];
說明:創建線程有多種方式,這里不做過多的介紹。
?
線程的開啟:
[self.thread start];
線程的運行和阻塞:
(1)設置線程阻塞1,阻塞2秒
? ? [NSThread sleepForTimeInterval:2.0];
? ?
(2)第二種設置線程阻塞2,以當前時間為基準阻塞4秒
? ? NSDate *date=[NSDate dateWithTimeIntervalSinceNow:4.0];
? ? [NSThread sleepUntilDate:date];
線程處理阻塞狀態時在內存中的表現情況:(線程被移出可調度線程池,此時不可調度)
線程的死亡:
當線程的任務結束,發生異常,或者是強制退出這三種情況會導致線程的死亡。
線程死亡后,線程對象從內存中移除。
二、代碼示例
代碼示例1:
1 //2 // YYViewController.m3 // 04-NSThread02-線程的狀態4 //5 // Created by apple on 14-6-23.6 // Copyright (c) 2014年 itcase. All rights reserved.7 //8 9 #import "YYViewController.h" 10 11 @interface YYViewController () 12 @property(nonatomic,strong)NSThread *thread; 13 14 @end 15 16 @implementation YYViewController 17 18 - (void)viewDidLoad 19 { 20 [super viewDidLoad]; 21 22 //創建線程 23 self.thread=[[NSThread alloc]initWithTarget:self selector:@selector(test) object:nil]; 24 //設置線程的名稱 25 [self.thread setName:@"線程A"]; 26 } 27 //當手指按下的時候,開啟線程 28 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 29 { 30 //開啟線程 31 [self.thread start]; 32 } 33 34 -(void)test 35 { 36 //獲取線程 37 NSThread *current=[NSThread currentThread]; 38 NSLog(@"test---打印線程---%@",self.thread.name); 39 NSLog(@"test---線程開始---%@",current.name); 40 41 //設置線程阻塞1,阻塞2秒 42 NSLog(@"接下來,線程阻塞2秒"); 43 [NSThread sleepForTimeInterval:2.0]; 44 45 //第二種設置線程阻塞2,以當前時間為基準阻塞4秒 46 NSLog(@"接下來,線程阻塞4秒"); 47 NSDate *date=[NSDate dateWithTimeIntervalSinceNow:4.0]; 48 [NSThread sleepUntilDate:date]; 49 for (int i=0; i<20; i++) { 50 NSLog(@"線程--%d--%@",i,current.name); 51 52 } 53 NSLog(@"test---線程結束---%@",current.name); 54 } 55 56 @end打印查看:
代碼示例2(退出線程):
1 //2 // YYViewController.m3 // 04-NSThread02-線程的狀態4 //5 // Created by apple on 14-6-23.6 // Copyright (c) 2014年 itcase. All rights reserved.7 //8 9 #import "YYViewController.h" 10 11 @interface YYViewController () 12 @property(nonatomic,strong)NSThread *thread; 13 14 @end 15 16 @implementation YYViewController 17 18 - (void)viewDidLoad 19 { 20 [super viewDidLoad]; 21 22 //創建線程 23 self.thread=[[NSThread alloc]initWithTarget:self selector:@selector(test) object:nil]; 24 //設置線程的名稱 25 [self.thread setName:@"線程A"]; 26 } 27 //當手指按下的時候,開啟線程 28 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 29 { 30 //開啟線程 31 [self.thread start]; 32 } 33 34 -(void)test 35 { 36 //獲取線程 37 NSThread *current=[NSThread currentThread]; 38 NSLog(@"test---打印線程---%@",self.thread.name); 39 NSLog(@"test---線程開始---%@",current.name); 40 41 //設置線程阻塞1,阻塞2秒 42 NSLog(@"接下來,線程阻塞2秒"); 43 [NSThread sleepForTimeInterval:2.0]; 44 45 //第二種設置線程阻塞2,以當前時間為基準阻塞4秒 46 NSLog(@"接下來,線程阻塞4秒"); 47 NSDate *date=[NSDate dateWithTimeIntervalSinceNow:4.0]; 48 [NSThread sleepUntilDate:date]; 49 for (int i=0; i<20; i++) { 50 NSLog(@"線程--%d--%@",i,current.name); 51 if (5==i) { 52 //結束線程 53 [NSThread exit]; 54 } 55 56 } 57 NSLog(@"test---線程結束---%@",current.name); 58 } 59 60 @end打印示例:
注意:人死不能復生,線程死了也不能復生(重新開啟),如果在線程死亡之后,再次點擊屏幕嘗試重新開啟線程,則程序會掛。
?
轉載于:https://www.cnblogs.com/sunflower-lhb/p/4914893.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的iOS开发多线程篇—线程的状态的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 夺命雷公狗jquery---22-bin
- 下一篇: GCD牛逼的中枢调度器