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

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

網絡開始---多線程---NSThread-01-基本使用(了解)(二)

2019-11-14 18:18:33
字體:
來源:轉載
供稿:網友
 1 #import "HMViewController.h" 2  3 @interface HMViewController () 4  5 @end 6  7 @implementation HMViewController 8  9 - (void)viewDidLoad10 {11     [super viewDidLoad];12     // Do any additional setup after loading the view, typically from a nib.13 }14 15 //下載操作,16 - (void)download:(NSString *)url17 {18     NSLog(@"下載東西---%@---%@", url, [NSThread currentThread]);19     20     21     22     23     24 }25 26 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event27 {28     [self createThread3];29 }30 31 /**32  * 創建線程的方式333  隱式創建線程并自動啟動線程34  */35 - (void)createThread336 {37 //    這2個不會創建線程,在當前線程中執行38 //    [self performSelector:@selector(download:) withObject:@"http://c.gif"];39 //    [self download:@"http://c.gif"];40     41     //這個隱式創建線程,在后臺會自動創建一條子線程并執行download方法,42     [self performSelectorInBackground:@selector(download:) withObject:@"http://c.gif"];43 }44 45 /**46  * 創建線程的方式247  創建線程后自動啟動線程48  */49 - (void)createThread250 {51     //從當前線程中分離出一條新的線程52     [NSThread detachNewThreadSelector:@selector(download:) toTarget:self withObject:@"http://a.jpg"];53 }54 55 /**56  * 創建線程的方式157  創建線程后不會自動啟動,需要寫一句啟動才會啟動線程58  這種創建線程的方法是3種當中最好的,可以對線程進行詳細的設置59  60  */61 - (void)createThread162 {63     // 創建線程64     NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(download:) object:@"http://b.png"];65     66     //線程的名字,打印線程的時候可以看到打印的線程的名字67     thread.name = @"下載線程";68     69     object://你開辟線程要執行的方法要傳的參數,比如這里傳一個url,傳到download方法里70     //這個參數是要從當前線程傳到子線程中去的71     72     // 啟動線程(調用self的download方法) 只有啟動線程才會調用self的download方法73     [thread start];  //必須有這一句才能啟動線程74     //并且執行過程是在子線程中執行的,就是新開辟的一條線程 把耗時操作成功放到子線程中去75     76     [NSThread mainThread];//獲得主線程77    NSThread *current=[NSThread currentThread];//獲得當前線程78     79     80     [thread isMainThread];//判斷當前線程(新創建線程)是否為主線程,返回Bool值81     [NSThread isMainThread];// 判斷當前執行代碼的方法是否在主線程,返回Bool值82     83 }84 85 @end

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 连江县| 蓬安县| 上林县| 民乐县| 石嘴山市| 伊宁县| 五华县| 滁州市| 天祝| 新营市| 江源县| 福泉市| 纳雍县| 商洛市| 崇明县| 宁河县| 循化| 上杭县| 江达县| 施甸县| 堆龙德庆县| 广东省| 上蔡县| 留坝县| 昆山市| 重庆市| 双江| 客服| 晋城| 女性| 余江县| 栖霞市| 鹤山市| 通榆县| 陆良县| 金华市| 陆河县| 永顺县| 贡觉县| 隆林| 汉源县|