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

首頁 > 學院 > 開發設計 > 正文

iOS多線程實現2-NSThread

2019-11-14 18:26:47
字體:
來源:轉載
供稿:網友

  NSThread是輕量級的多線程開發,OC語言編寫,更加面向對象,使用起來也并不復雜,但是使用NSThread需要自己管理線程生命周期。在iOS開發中很少使用它來創建一個線程,但是經常使用它做一些延時操作,獲取當前線程,線程間通訊等等。

  但是,在線程同步方面,控制線程執行順序比較麻煩,線程同步對數據的加鎖會有一定的系統開銷,且創建線程也會增加系統的開銷。

  1 創建方法

  有多種創建方法,- (void)runDemo:(NSString *)param;為要執行的示例方法。

- (void)runDemo:(NSString *)param {    NSThread *current = [NSThread currentThread];    NSLog(@"%@---%@ is running", param, current);}/// 方式1 自動創建線程, 并且自動啟動- (void)threadCreateOne {    // 在另一個線程執行 runDemo:    [self performSelectorInBackground:@selector(runDemo:) withObject:@"One"];}/// 方式2 創建完線程直接(自動)啟動- (void)threadCreateTwo {    [NSThread detachNewThreadSelector:@selector(runDemo:) toTarget:self withObject:@"Two"];}/// 方式3  先創建初始化線程,然后start開啟線程- (void)threadCreateThree {    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(runDemo:) object:@"Three"];    // 可以設置線程名字    thread.name = @"名字";    // 開啟線程    [thread start];}

   下面為測試代碼,以及打印結果,我們調用的順序是One->Two->Three,但是打印結果是Two->Three->One,因為線程啟動后僅僅處于就緒狀態,實際是否執行要由CPU根據當前狀態調度,即執行順序是無序的,這也是多線程的特點。

/// 點擊屏幕后創建線程- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    [self threadCreateOne];    [self threadCreateTwo];    [self threadCreateThree];}打印結果:2015-08-27 16:27:34.974 01test[1183:76667] Two---<NSThread: 0x7ff250e1c9a0>{number = 3, name = (null)} is running2015-08-27 16:27:34.974 01test[1183:76668] Three---<NSThread: 0x7ff250e168a0>{number = 4, name = 名字} is running2015-08-27 16:27:34.974 01test[1183:76666] One---<NSThread: 0x7ff250f406a0>{number = 2, name = (null)} is running

  2 常用函數

  獲取當前線程,獲取主線程,判斷當前線程是否為主線程。

// 獲取當前線程NSThread *current = [NSThread currentThread];// 獲取主線程current = [NSThread mainThread];// 判斷當前線程是否為主線程BOOL isMain = [current isMainThread];

   暫停線程,下面代碼為2種方法均讓當前線程睡5s

[NSThread sleepForTimeInterval:5];NSDate *date = [NSDate dateWithTimeInterval:5 sinceDate:[NSDate date]];[NSThread sleepUntilDate:date];

  獲取線程的狀態,分別為:正在執行、已經完成、已經取消。

@PRoperty (readonly, getter=isExecuting) BOOL executing;@property (readonly, getter=isFinished)  BOOL finished;@property (readonly, getter=isCancelled) BOOL cancelled;

  在指定的線程(已存在的線程)、主線程、當前線程上執行方法。這種比較常用,通常用于線程間通訊,且它們是NSObject的擴展方法,使用起來很方便。

// 在指定的線程執行runDemo:方法,最后的YES代表:下面的代碼會阻塞,等runDemo:方法在thread線程執行完畢后,才會執行下面代碼的下一行代碼,設為NO則不阻塞。那么runDemo:與下一行代碼的執行順序不確定[self performSelector:@selector(runDemo:) onThread:thread withObject:nil waitUntilDone:YES];// 在主線程執行runDemo:方法,YES參數同上[self performSelectorOnMainThread:@selector(runDemo:) withObject:nil waitUntilDone:YES];// 在當前線程執行方法[self performSelector:@selector(run) withObject:nil];

  退出線程

+ (void)exit;

  線程優先級相關,優先級范圍是0.0 ~ 1.0,默認0.5,值越大,優先級越高。開發時,很少使用優先級,如果設置優先級且使用線程鎖會造成優先級翻轉,需要特備注意。

+ (double)threadPriority;+ (BOOL)setThreadPriority:(double)p;

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 无棣县| 齐河县| 沁源县| 溧阳市| 卢龙县| 嘉善县| 静安区| 楚雄市| 陇川县| 渭南市| 蒙自县| 西华县| 永嘉县| 吉木乃县| 昔阳县| 鲜城| 枣阳市| 盐源县| 广昌县| 留坝县| 沭阳县| 治县。| 丰宁| 乾安县| 凤凰县| 洪雅县| 贺兰县| 延庆县| 剑河县| 潍坊市| 松原市| 涪陵区| 寿光市| 托克逊县| 衡阳市| 大竹县| 清水县| 无极县| 扎鲁特旗| 高密市| 拜城县|