iosGCD多線程之建立多線程

來源:互聯網
上載者:User

標籤:ios   多線程   線程   編程   ios開發   

喔盡量分成一小節一小節的寫。這樣也難讓大家看的清楚些。我這裡有三種建立線程的方法。代碼如下


#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

}

//當使用者點擊螢幕,執行線程

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

       [selftestThread];

}


-(void)testThread

{

    

    //取到當前線程,在這裡就是主線程

    NSThread *curThread = [NSThreadcurrentThread];

    NSLog(@"curThread = %@",curThread);

    

//    //取到主線程的方法

//    NSThread *mainThread = [NSThread mainThread];

//    NSLog(@"mainThread = %@",mainThread);

//    

    

    [selfcreateThread1];

    

}



-(void)createThread1

{

    //一個NSThread對象就是一個線程

    // 參數1,2 :指定線程中由參數1調用參數2的方法

    // 參數3 :給參數2指定的方法傳遞實參

    NSThread *thread = [[NSThreadalloc]initWithTarget:selfselector:@selector(threadMain:)object:@"線程建立方式1"];

    

    [thread setName:@"我叫二蛋"];

    [thread start];

}

-(void)createThread2

{

    

    [NSThreaddetachNewThreadSelector:@selector(threadMain:)toTarget:selfwithObject:@"線程建立方式2"];

    

    

}

-(void)createThread3

{

    

    

    [selfperformSelectorInBackground:@selector(threadMain:)withObject:@"線程建立方式3"];

}

-(void)threadMain:(id)obj

{

    for (int i =0; i<1000; i++) {

        NSLog(@"i = %d,obj = %@,thread = %@",i,obj,[NSThread currentThread]);

    }

    

    

}


當然大家還是需要多用,就像吃飯,吃的多了還能挑刺,繼續下一篇,今天晚上3更。哈哈

iosGCD多線程之建立多線程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.