GCD(1)

來源:互聯網
上載者:User

標籤:使用   os   資料   art   io   for   

```objc

import "ViewController.h"

int threadNumber = 0;

int newThingNumber = 0;

@interface ViewController ()

@end

@implementation ViewController

  • (void)viewDidLoad{[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.}

//1如果主線程和子線程是並行的,倚靠CPU的調度,主線程和子線程都有機會得到執行。//2如果要在子線程裡面改變主線程裡面的資料,就在裡面的最後 一個參數使用傳地調用(void *)&c,否則使用傳值調用(void *)c//3使用pthread_join的目的在於:怕主線程執行的過快,子線程還沒有執行,主線程就執行完畢並且退出了。

  • (IBAction)doSomething:(id)sender {

// 如果是在單線程裡執行,則按紐狀態的執行中字樣會一直持續10秒// 目前,對於iOS來說, 程式的GUI,是在主線程裡執行的。換句話:所有UI程式都應該在主線程裡運行// 預設情況, button的回調方法, 是在主線程運行著

// [self doThing:nil];

// 開啟新的線程,意思是同步執行(同步)// 第一種辦法來開啟一個新的線程// 第一個參數:開啟後台線程, 需要執行的代碼(任務), 第二個參數是線程通訊參數

// [self performSelectorInBackground:@selector(doThing:) withObject:@"Hellowangdelong"];

//    第二種開啟線程的方法

// 啟動一個子線程,並使用doThing:作為線程的進入點(線程任務)// 第二個參數:指定是哪個對象定義了線程的進入點(線程作務),// 第三個參數:傳遞給子線程的資料(實現線程這間通訊的手段)

// [NSThread detachNewThreadSelector:@selector(doThing:) toTarget:self withObject:@"Hello"];

//    第三種開啟線程的方法 //    建立一個線程對象, 可以實現對線程何時開始啟動並執行控制。

// NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(doThing:) object:@"qingYun"]; // 給線程起一個名字, 用來標識這個線程。// thread.name = @"FirstQingYunThread";

 //    啟動線程

// [thread start];

//    第四種開啟線程的方法 //    建立一個隊列

// NSOperationQueue *queque = [[NSOperationQueue alloc] init]; // 建立子任務, 定義子任務必須是NSOperation的子類// NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(doThing:) object:@"HelloOperationQueue"]; // 當把任務添加到隊列後, 自動開啟線程,// [queque addOperation:operation]; // 在同一個NSOperationQueue隊列不同出現同一個任務對象,否則會出現異常// [queque addOperation:operation];

//  第五種開啟線程的方法

// NSOperationQueue *queque = [[NSOperationQueue alloc] init];// NSBlockOperation *oper = [NSBlockOperation blockOperationWithBlock:^{// NSLog(@"do thing...");// threadNumber++;// // 表示當前執行doThing的線程對象// NSThread *currentThread = [NSThread currentThread];//
// // isMainThread:如果是主線程的話, 則返回YES,否則NO// if ([currentThread isMainThread]) {// NSLog(@"Main thread...%d",threadNumber);// }else// {// NSLog(@"Peer thread...%d",threadNumber);// }//
// [NSThread sleepForTimeInterval:10];//讓處理器休眠10秒// NSLog(@"finish doThing");//
// }];//
// [queque addOperation:oper];//

//    第六種開啟線程的方法//    GCD//    Serial 隊列  (NULL, nil Nil NSNull)的區別是什嗎?第一個參數是唯一標識建立的隊列對象的
if 0
dispatch_queue_t queue = dispatch_queue_create("wangdelong", NULL);

// 非同步執行GCD的block(線程任務)// 1?對於serial隊列來說,主線程可以理解為一個queue, queue 與queue之間肯定非同步執行,// 2?對於同一個serial隊列時的子任務,同步執行,遵循FIFO

dispatch_async(queue, ^{    NSLog(@"doThing...");    threadNumber++;    //     表示當前執行doThing的線程對象    NSThread *currentThread = [NSThread currentThread];    //如果是在主線程裡執行的話, 列印出在主線程。    //    否則,則列印是對等線程    //    判斷是主線程還是對等線程(非同步)    if ([currentThread isMainThread]) {        NSLog(@"Main thread is :%d",threadNumber);    }else{        NSLog(@"Peer thread id :%d",threadNumber);    }    //    //讓處理器休眠10秒    [NSThread sleepForTimeInterval:5];    NSLog(@"finish working");});

// dispatchqueuet secondqueue = dispatchqueuecreate("wangdelong", NULL);

dispatch_async(queue, ^{    NSLog(@" doThing...");    threadNumber++;    //     表示當前執行doThing的線程對象    NSThread *currentThread = [NSThread currentThread];    //如果是在主線程裡執行的話, 列印出在主線程。    //    否則,則列印是對等線程    //    判斷是主線程還是對等線程(非同步)    if ([currentThread isMainThread]) {        NSLog(@"secondMain thread is :%d",threadNumber);    }else{        NSLog(@"secondPeer thread id :%d",threadNumber);    }    //    //讓處理器休眠10秒    [NSThread sleepForTimeInterval:5];    NSLog(@"second finish working");});
endif

// Concurrent 隊列 同一個隊列裡的多個子任務之間也是並發執行的(注意這是與serial隊列的區別)// 同一個隊列的不同子任務並發執行// 不同的隊列,他的調度順序是根據優先順序來確定, 優先順序高的, 先調度。// 第一個參數:用於標識隊列的優先順序,有如下幾種:// #define DISPATCHQUEUEPRIORITYHIGH// #define DISPATCHQUEUEPRIORITYDEFAULT// #define DISPATCHQUEUEPRIORITYLOW// #define DISPATCHQUEUEPRIORITYBACKGROUND// 第二個參數:寫0就可以, 目前保留

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);dispatch_queue_t secondqueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);dispatch_async(queue, ^{    NSLog(@"first doThing...");    threadNumber++;    //     表示當前執行doThing的線程對象    NSThread *currentThread = [NSThread currentThread];    //如果是在主線程裡執行的話, 列印出在主線程。    //    否則,則列印是對等線程    //    判斷是主線程還是對等線程(非同步)    if ([currentThread isMainThread]) {        NSLog(@"first Main thread is :%d",threadNumber);    }else{        NSLog(@"first Peer thread id :%d",threadNumber);    }    //    //讓處理器休眠10秒    [NSThread sleepForTimeInterval:5];    NSLog(@"first finish working");});dispatch_sync(secondqueue, ^{    NSLog(@"second doThing...");    threadNumber++;    //     表示當前執行doThing的線程對象    NSThread *currentThread = [NSThread currentThread];    //如果是在主線程裡執行的話, 列印出在主線程。    //    否則,則列印是對等線程    //    判斷是主線程還是對等線程(非同步)    if ([currentThread isMainThread]) {        NSLog(@"second Main thread is :%d",threadNumber);    }else{        NSLog(@"second Peer thread id :%d",threadNumber);    }    //    //讓處理器休眠10秒    [NSThread sleepForTimeInterval:5];    NSLog(@"second finish working");});//    表示當前執行doThing的線程對象NSThread *currentThread = [NSThread currentThread];//    isMainThread:如果是主線程的話, 則返回YES,否則NOif ([currentThread isMainThread]) {    NSLog(@"This is Main thread...%d",threadNumber);}else{    NSLog(@"This is Peer thread...%d",threadNumber);}

}

-(void)doThing:(id)object{

NSLog(@"doThing...:%@",object);threadNumber++;

// 表示當前執行doThing的線程對象 NSThread *currentThread = [NSThread currentThread];

//如果是在主線程裡執行的話, 列印出在主線程。// 否則,則列印是對等線程// 判斷是主線程還是對等線程(非同步) if ([currentThread isMainThread]) { NSLog(@"Main thread is :%d",threadNumber);

}else{    NSLog(@"Peer thread id :%d",threadNumber);}

// //讓處理器休眠10秒 [NSThread sleepForTimeInterval:10];

NSLog(@"finish working");

}

//重新定義一個button,- (IBAction)doNewThing:(id)sender {

NSLog(@"do another thing...:%d",newThingNumber);newThingNumber++;if ([NSThread isMainThread]) {    NSLog(@"do another thing is main thread");}else{    NSLog(@"do another thing is peer thread");}[NSThread sleepForTimeInterval:3];NSLog(@"do another thing finished");

}

@end

```

聯繫我們

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