iOS GCD三種建立執行緒的方式
- (IBAction)gcd1:(id)sender {
//用於全域性併發佇列
dispatch_queue_t qGlobal=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(qGlobal, ^{
int x=0;
while (x<20) {
NSLog(@"%@",[NSThread currentThread]);
[NSThreadsleepForTimeInterval:2];
//
[selfperformSelector:@selector(showInfo:) onThread:[NSThreadmainThread] withObject:@"global-queue dispathc-asyn"waitUntilDone:YES];
x++;
}
});
}
- (IBAction)gcd2:(id)sender {
//此佇列與序列佇列配合使用
dispatch_queue_t qCreate=dispatch_queue_create("q", nil);
dispatch_sync
int x=0;
while (x<20) {
NSLog(@"%@",[NSThread currentThread]);
[NSThreadsleepForTimeInterval:2];
//多執行緒間引數傳遞
[selfperformSelector:@selector(showInfo:) onThread:[NSThreadmainThread] withObject:@"create-queue dispathc-syn"waitUntilDone:
x++;
}
});
}
- (IBAction)gcd3:(id)sender {
//使用主佇列建立的執行緒是沒有意義的,因為一致不執行,這個還的請教高人指點謝謝
dispatch_queue_t qMain=dispatch_get_main_queue();
dispatch_async(qMain, ^{
NSLog(@"test asyn+qMain :%@",[NSThreadcurrentThread]);
});
dispatch_sync(qMain, ^{
NSLog(@"this is qmain create thread %@",[NSThreadcurrentThread]);
});
}