Skip to content

Instantly share code, notes, and snippets.

@mactive
Created July 21, 2016 23:52
Show Gist options
  • Save mactive/497696faaeb8772f7d72cc83995b0c0d to your computer and use it in GitHub Desktop.
Save mactive/497696faaeb8772f7d72cc83995b0c0d to your computer and use it in GitHub Desktop.
-(void)gcdTest1
{
dispatch_queue_t concurrentDispatchQueue=dispatch_queue_create("com.test.queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(concurrentDispatchQueue, ^{
NSLog(@"1");
});
dispatch_async(concurrentDispatchQueue, ^{
sleep(2);
NSLog(@"2");
});
dispatch_async(concurrentDispatchQueue, ^{
sleep(1);
NSLog(@"3");
});
}
// 创建一个并发队列
// 然后异步的去执行
DISPATCH_QUEUE_CONCURRENT + async 才会多个线程
2016-07-21 12:57:48.883 ReactiveCocoaPractice[7139:11438400] 1
2016-07-21 12:57:49.887 ReactiveCocoaPractice[7139:11438411] 3
2016-07-21 12:57:50.888 ReactiveCocoaPractice[7139:11438408] 2
各种组合下的输出
DISPATCH_QUEUE_SERIAL + sync
2016-07-21 12:58:43.713 ReactiveCocoaPractice[7213:11440506] 1
2016-07-21 12:58:45.715 ReactiveCocoaPractice[7213:11440506] 2
2016-07-21 12:58:46.716 ReactiveCocoaPractice[7213:11440506] 3
DISPATCH_QUEUE_CONCURRENT + sync
2016-07-21 12:56:33.542 ReactiveCocoaPractice[7013:11434963] 1
2016-07-21 12:56:35.543 ReactiveCocoaPractice[7013:11434963] 2
2016-07-21 12:56:36.544 ReactiveCocoaPractice[7013:11434963] 3
DISPATCH_QUEUE_SERIAL + async
2016-07-21 12:57:20.338 ReactiveCocoaPractice[7090:11437140] 1
2016-07-21 12:57:22.343 ReactiveCocoaPractice[7090:11437140] 2
2016-07-21 12:57:23.347 ReactiveCocoaPractice[7090:11437140] 3
DISPATCH_QUEUE_CONCURRENT + async 才会多个线程
2016-07-21 12:57:48.883 ReactiveCocoaPractice[7139:11438400] 1
2016-07-21 12:57:49.887 ReactiveCocoaPractice[7139:11438411] 3
2016-07-21 12:57:50.888 ReactiveCocoaPractice[7139:11438408] 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment