日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS开发多线程篇—线程的状态

發布時間:2023/11/29 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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开发多线程篇—线程的状态的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。