iOS開發多線程篇—建立線程

來源:互聯網
上載者:User

標籤:

iOS開發多線程篇—建立線程

一、建立和啟動線程簡單說明

一個NSThread對象就代表一條線程

建立、啟動線程

(1) NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];

[thread start];

// 線程一啟動,就會線上程thread中執行self的run方法

 

主線程相關用法

+ (NSThread *)mainThread; // 獲得主線程

- (BOOL)isMainThread; // 是否為主線程

+ (BOOL)isMainThread; // 是否為主線程

 

其他用法

獲得當前線程

NSThread *current = [NSThread currentThread];

 

線程的調度優先順序:調度優先順序的取值範圍是0.0 ~ 1.0,預設0.5,值越大,優先順序越高

+ (double)threadPriority;

+ (BOOL)setThreadPriority:(double)p;

 

設定線程的名字

- (void)setName:(NSString *)n;

- (NSString *)name;

 

其他建立線程的方式

(2)建立線程後自動啟動線程   [NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];

(3)隱式建立並啟動線程  [self performSelectorInBackground:@selector(run) withObject:nil];

上述2種建立線程方式的優缺點

優點:簡單快捷

缺點:無法對線程進行更詳細的設定

二、程式碼範例

1.使用古老的方式建立

////  YYViewController.m//////  Created by apple on 14-6-23.//  Copyright (c) 2014年 itcase. All rights reserved.//#import "YYViewController.h"#import <pthread.h>@interface YYViewController ()- (IBAction)btnClick;@end @implementation YYViewController - (void)viewDidLoad{    [super viewDidLoad];} //按鈕的點擊事件- (IBAction)btnClick {    //1.擷取當前線程    NSThread *current=[NSThread currentThread];    //主線程    NSLog(@"btnClick----%@",current);       //2.使用for迴圈執行一些耗時操作   pthread_t thread;    pthread_create(&thread, NULL, run, NULL);}//c語言函數void *run(void *data){    //擷取當前線程,是新建立出來的線程    NSThread *current=[NSThread currentThread];    for (int i=0; i<10000; i++) {        NSLog(@"btnClick---%d---%@",i,current);    }    return NULL;}//多個線程,點擊按鈕執行按鈕調用方法的時候,主線程沒有被阻塞@end

實現效果:

 

列印結果:

2.使用NSThread建立線程

////  YYViewController.m//////  Created by apple on 14-6-23.//  Copyright (c) 2014年 itcase. All rights reserved.//#import "YYViewController.h"#import <pthread.h>@interface YYViewController ()- (IBAction)btnClick;@end@implementation YYViewController - (void)viewDidLoad{    [super viewDidLoad];}//按鈕的點擊事件- (IBAction)btnClick {    //1.擷取當前線程    NSThread *current=[NSThread currentThread];    //主線程    NSLog(@"btnClick----%@",current);    //擷取主線程的另外一種方式   NSThread *main=[NSThread mainThread];    NSLog(@"主線程-------%@",main);    //2.執行一些耗時操作    [self creatNSThread];//    [self creatNSThread2];//    [self creatNSThread3];} /** * NSThread建立線程方式1 * 1> 先建立初始化線程 * 2> start開啟線程 */-(void)creatNSThread{    NSThread  *thread=[[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"線程A"];    //為線程設定一個名稱    thread.name=@"線程A";     //開啟線程    [thread start];      NSThread  *thread2=[[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"線程B"];    //為線程設定一個名稱    thread2.name=@"線程B";   //開啟線程    [thread2 start];} /** * NSThread建立線程方式2*建立完線程直接(自動)啟動 */-(void)creatNSThread2{//    NSThread *thread=[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"建立完線程直接(自動)啟動"];    [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"建立完線程直接(自動)啟動"];}/** * NSThread建立線程方式3 * 隱式建立線程, 並且直接(自動)啟動 */-(void)creatNSThread3{    //在後台線程中執行===在子線程中執行    [self performSelectorInBackground:@selector(run:) withObject:@"隱式建立"];}-(void)run:(NSString *)str{   //擷取當前線程    NSThread *current=[NSThread currentThread];    //列印輸出    for (int i=0; i<10; i++) {       NSLog(@"run---%@---%@",current,str);    }}@end

調用線程1,列印結果為:

 

調用線程2 

 

調用線程3 

 

 

iOS開發多線程篇—建立線程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.