国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

為你分析iOS多線程開發(fā)

2020-02-19 15:53:56
字體:
供稿:網(wǎng)友

作為一名iOS開發(fā)人員,無論你是新手還是大師級別的人,相信對多線程都會陌生,下文是武林技術(shù)頻道小編為你分析iOS多線程開發(fā),一起進入下文了解一下吧!

為你分析iOS多線程開發(fā)

一、簡單介紹

線程的創(chuàng)建:

?


self.thread=[[NSThread alloc]initWithTarget:self selector:@selector(test) object:nil];

?

?

?

說明:創(chuàng)建線程有多種方式,這里不做過多的介紹。

線程的開啟:

?


[self.thread start];

?

?

?

線程的運行和阻塞:

(1)設(shè)置線程阻塞1,阻塞2秒

[NSThread sleepForTimeInterval:2.0];

(2)第二種設(shè)置線程阻塞2,以當(dāng)前時間為基準(zhǔn)阻塞4秒

?


NSDate *date=[NSDate dateWithTimeIntervalSinceNow:4.0];
[NSThread sleepUntilDate:date];

?

線程處理阻塞狀態(tài)時在內(nèi)存中的表現(xiàn)情況:(線程被移出可調(diào)度線程池,此時不可調(diào)度)

線程的死亡:

當(dāng)線程的任務(wù)結(jié)束,發(fā)生異常,或者是強制退出這三種情況會導(dǎo)致線程的死亡。

線程死亡后,線程對象從內(nèi)存中移除。

二、代碼示例

代碼示例1:

//// YYViewController.m// -NSThread-線程的狀態(tài)//// Created by apple on --.// Copyright (c) 年 itcase. All rights reserved.//#import "YYViewController.h"@interface YYViewController ()@property(nonatomic,strong)NSThread *thread;@end@implementation YYViewController- (void)viewDidLoad{[super viewDidLoad];//創(chuàng)建線程self.thread=[[NSThread alloc]initWithTarget:self selector:@selector(test) object:nil];//設(shè)置線程的名稱[self.thread setName:@"線程A"];}//當(dāng)手指按下的時候,開啟線程-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{//開啟線程[self.thread start];}-(void)test{//獲取線程NSThread *current=[NSThread currentThread];NSLog(@"test---打印線程---%@",self.thread.name);NSLog(@"test---線程開始---%@",current.name);//設(shè)置線程阻塞,阻塞秒NSLog(@"接下來,線程阻塞秒");[NSThread sleepForTimeInterval:.];//第二種設(shè)置線程阻塞,以當(dāng)前時間為基準(zhǔn)阻塞秒NSLog(@"接下來,線程阻塞秒");NSDate *date=[NSDate dateWithTimeIntervalSinceNow:.];[NSThread sleepUntilDate:date];for (int i=; i

打印查看:

代碼示例2(退出線程):

//// YYViewController.m// -NSThread-線程的狀態(tài)//// Created by apple on --.// Copyright (c) 年 itcase. All rights reserved.//#import "YYViewController.h"@interface YYViewController ()@property(nonatomic,strong)NSThread *thread;@end@implementation YYViewController- (void)viewDidLoad{[super viewDidLoad];//創(chuàng)建線程self.thread=[[NSThread alloc]initWithTarget:self selector:@selector(test) object:nil];//設(shè)置線程的名稱[self.thread setName:@"線程A"];}//當(dāng)手指按下的時候,開啟線程-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{//開啟線程[self.thread start];}-(void)test{//獲取線程NSThread *current=[NSThread currentThread];NSLog(@"test---打印線程---%@",self.thread.name);NSLog(@"test---線程開始---%@",current.name);//設(shè)置線程阻塞,阻塞秒NSLog(@"接下來,線程阻塞秒");[NSThread sleepForTimeInterval:.];//第二種設(shè)置線程阻塞,以當(dāng)前時間為基準(zhǔn)阻塞秒NSLog(@"接下來,線程阻塞秒");NSDate *date=[NSDate dateWithTimeIntervalSinceNow:.];[NSThread sleepUntilDate:date];for (int i=; i

打印示例:

注意:人死不能復(fù)生,線程死了也不能復(fù)生(重新開啟),如果在線程死亡之后,再次點擊屏幕嘗試重新開啟線程,則程序會掛。

以上內(nèi)容是武林技術(shù)頻道小編給大家介紹的為你分析iOS多線程開發(fā) ,希望大家喜歡。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 巴里| 子长县| 永善县| 城市| 临邑县| 赞皇县| 开远市| 合川市| 定西市| 新平| 七台河市| 平顶山市| 都安| 大方县| 文昌市| 邹城市| 泾阳县| 新化县| 武安市| 苍溪县| 高碑店市| 台江县| 昌图县| 平度市| 乌苏市| 长宁区| 安丘市| 武鸣县| 岐山县| 孝感市| 郸城县| 句容市| 无锡市| 顺义区| 奎屯市| 全椒县| 古蔺县| 安仁县| 巴青县| 小金县| 镇安县|