1. 单个队列
dispatch_queue_t serialQueue = dispatch_queue_create("serial", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t concurrentQueue = dispatch_queue_create("concur", DISPATCH_QUEUE_CONCURRENT);
dispatch_queue_t mainQueue = dispatch_get_main_queue();
NSLog(@"--主队列 %@", [NSThread currentThread]);
dispatch_sync(serialQueue, ^{
NSLog(@"--同步串行 %@", [NSThread currentThread]);
});
dispatch_sync(concurrentQueue, ^{
NSLog(@"--同步并行: %@", [NSThread currentThread]);
});
dispatch_async(serialQueue, ^{
NSLog(@"--异步串行: %@", [NSThread currentThread]);
});
dispatch_async(concurrentQueue, ^{
NSLog(@"--异步并行: %@", [NSThread currentThread]);
});
dispatch_async(mainQueue, ^{
NSLog(@"--异步主队列: %@", [NSThread currentThread]);
});
dispatch_sync(mainQueue, ^{
NSLog(@"--主线程同步: %@", [NSThread currentThread]);
});
2. 队列嵌套
NSLog(@"--主线程: %@", [NSThread currentThread]);
dispatch_queue_t serialQueue = dispatch_queue_create("com.objcc.serial1", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t concurrentQueue = dispatch_queue_create("com.objcc.concurrent1", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(concurrentQueue, ^{
NSLog(@"--异步并发: %@", [NSThread currentThread]);
dispatch_sync(concurrentQueue, ^{
NSLog(@"--同步并发: %@", [NSThread currentThread]);
});
});
dispatch_async(concurrentQueue, ^{
NSLog(@"--异步并发1: %@",[NSThread currentThread]);
dispatch_async(concurrentQueue, ^{
NSLog(@"--异步并发2: %@",[NSThread currentThread]);
});
});
dispatch_sync(concurrentQueue, ^{
NSLog(@"--同步并发1: %@",[NSThread currentThread]);
dispatch_sync(concurrentQueue, ^{
NSLog(@"--同步并发1: %@",[NSThread currentThread]);
});
});
dispatch_sync(concurrentQueue, ^{
NSLog(@"--同步并发1: %@",[NSThread currentThread]);
dispatch_async(concurrentQueue, ^{
NSLog(@"--异步并发2: %@",[NSThread currentThread]);
});
});
dispatch_async(serialQueue, ^{
NSLog(@"--异步串行1: %@",[NSThread currentThread]);
dispatch_sync(serialQueue, ^{
NSLog(@"--同步串发2: %@",[NSThread currentThread]);
});
});
dispatch_async(serialQueue, ^{
NSLog(@"--异步串行1: %@",[NSThread currentThread]);
dispatch_async(serialQueue, ^{
NSLog(@"--异步串行2: %@",[NSThread currentThread]);
});
});
dispatch_sync(serialQueue, ^{
NSLog(@"--同步串行1: %@",[NSThread currentThread]);
dispatch_sync(serialQueue, ^{
NSLog(@"--同步串行1: %@",[NSThread currentThread]);
});
});
dispatch_sync(serialQueue, ^{
NSLog(@"--同步串行1: %@",[NSThread currentThread]);
dispatch_async(serialQueue, ^{
NSLog(@"--异步串行2: %@",[NSThread currentThread]);
});
});