考察点:
- NSLock锁运用
- 异步转同步
- 并发队列
启动三个线程A,B,C,打印10次 按照ABC的顺序输出
dispatch_queue_t queueA = dispatch_queue_create("queuea", DISPATCH_QUEUE_CONCURRENT);dispatch_async(queueA, ^{for (int i = 0; i<10; i++) {NSLog(@"A======= %@",@(i));}});dispatch_queue_t queueB = dispatch_queue_create("queueb", DISPATCH_QUEUE_CONCURRENT);dispatch_async(queueB, ^{for (int i = 0; i<10; i++) {NSLog(@"B======= %@",@(i));}});dispatch_queue_t queueC = dispatch_queue_create("queuec", DISPATCH_QUEUE_CONCURRENT);dispatch_async(queueC, ^{for (int i = 0; i<10; i++) {NSLog(@"C======= %@",@(i));NSLog(@" ");}});
利用NSLock实现
NSLock *lockA = [[NSLock alloc] init];NSLock *lockB = [[NSLock alloc] init];NSLock *lockC = [[NSLock alloc] init];[lockB lock];[lockC lock];dispatch_queue_t queueA = dispatch_queue_create("queuea", DISPATCH_QUEUE_CONCURRENT);dispatch_async(queueA, ^{for (int i = 0; i<10; i++) {[lockA lock];NSLog(@"A======= %@",@(i));[lockB unlock];}});dispatch_queue_t queueB = dispatch_queue_create("queueb", DISPATCH_QUEUE_CONCURRENT);dispatch_async(queueB, ^{for (int i = 0; i<10; i++) {[lockB lock];NSLog(@"B======= %@",@(i));[lockC unlock];}});dispatch_queue_t queueC = dispatch_queue_create("queuec", DISPATCH_QUEUE_CONCURRENT);dispatch_async(queueC, ^{for (int i = 0; i<10; i++) {[lockC lock];NSLog(@"C======= %@",@(i));NSLog(@" ");[lockA unlock];}});2019-06-20 01:06:23.384170+0800 xxtest[2808:669702] A======= 02019-06-20 01:06:23.384379+0800 xxtest[2808:669701] B======= 02019-06-20 01:06:23.384515+0800 xxtest[2808:669703] C======= 02019-06-20 01:06:23.384613+0800 xxtest[2808:669703]2019-06-20 01:06:23.384750+0800 xxtest[2808:669702] A======= 12019-06-20 01:06:23.384878+0800 xxtest[2808:669701] B======= 12019-06-20 01:06:23.384992+0800 xxtest[2808:669703] C======= 12019-06-20 01:06:23.385716+0800 xxtest[2808:669703]2019-06-20 01:06:23.386194+0800 xxtest[2808:669702] A======= 22019-06-20 01:06:23.386590+0800 xxtest[2808:669701] B======= 22019-06-20 01:06:23.387100+0800 xxtest[2808:669703] C======= 22019-06-20 01:06:23.387497+0800 xxtest[2808:669703]2019-06-20 01:06:23.387857+0800 xxtest[2808:669702] A======= 32019-06-20 01:06:23.388317+0800 xxtest[2808:669701] B======= 32019-06-20 01:06:23.388663+0800 xxtest[2808:669703] C======= 32019-06-20 01:06:23.388969+0800 xxtest[2808:669703]2019-06-20 01:06:23.389857+0800 xxtest[2808:669702] A======= 42019-06-20 01:06:23.390137+0800 xxtest[2808:669701] B======= 42019-06-20 01:06:23.390546+0800 xxtest[2808:669703] C======= 42019-06-20 01:06:23.390847+0800 xxtest[2808:669703]2019-06-20 01:06:23.391203+0800 xxtest[2808:669702] A======= 52019-06-20 01:06:23.391476+0800 xxtest[2808:669701] B======= 52019-06-20 01:06:23.391854+0800 xxtest[2808:669703] C======= 52019-06-20 01:06:23.392134+0800 xxtest[2808:669703]2019-06-20 01:06:23.392443+0800 xxtest[2808:669702] A======= 62019-06-20 01:06:23.392804+0800 xxtest[2808:669701] B======= 62019-06-20 01:06:23.393107+0800 xxtest[2808:669703] C======= 62019-06-20 01:06:23.393413+0800 xxtest[2808:669703]2019-06-20 01:06:23.393685+0800 xxtest[2808:669702] A======= 72019-06-20 01:06:23.394006+0800 xxtest[2808:669701] B======= 72019-06-20 01:06:23.394301+0800 xxtest[2808:669703] C======= 72019-06-20 01:06:23.394578+0800 xxtest[2808:669703]2019-06-20 01:06:23.394959+0800 xxtest[2808:669702] A======= 82019-06-20 01:06:23.395532+0800 xxtest[2808:669701] B======= 82019-06-20 01:06:23.395873+0800 xxtest[2808:669703] C======= 82019-06-20 01:06:23.396117+0800 xxtest[2808:669703]2019-06-20 01:06:23.396508+0800 xxtest[2808:669702] A======= 92019-06-20 01:06:23.396897+0800 xxtest[2808:669701] B======= 92019-06-20 01:06:23.397307+0800 xxtest[2808:669703] C======= 92019-06-20 01:06:23.397649+0800 xxtest[2808:669703]
利用信号量实现
dispatch_semaphore_t semaA = dispatch_semaphore_create(1);dispatch_semaphore_t semaB = dispatch_semaphore_create(0);dispatch_semaphore_t semaC = dispatch_semaphore_create(0);dispatch_queue_t queueA = dispatch_queue_create("queuea", DISPATCH_QUEUE_CONCURRENT);dispatch_async(queueA, ^{for (int i = 0; i<10; i++) {dispatch_semaphore_wait(semaA, DISPATCH_TIME_FOREVER);NSLog(@"A======= %@",@(i));dispatch_semaphore_signal(semaB);}});dispatch_queue_t queueB = dispatch_queue_create("queueb", DISPATCH_QUEUE_CONCURRENT);dispatch_async(queueB, ^{for (int i = 0; i<10; i++) {dispatch_semaphore_wait(semaB, DISPATCH_TIME_FOREVER);NSLog(@"B======= %@",@(i));dispatch_semaphore_signal(semaC);}});dispatch_queue_t queueC = dispatch_queue_create("queuec", DISPATCH_QUEUE_CONCURRENT);dispatch_async(queueC, ^{for (int i = 0; i<10; i++) {dispatch_semaphore_wait(semaC, DISPATCH_TIME_FOREVER);NSLog(@"C======= %@",@(i));NSLog(@" ");dispatch_semaphore_signal(semaA);}});2019-06-20 01:40:25.050013+0800 xxtest[2938:708288] A======= 02019-06-20 01:40:25.050183+0800 xxtest[2938:708285] B======= 02019-06-20 01:40:25.050297+0800 xxtest[2938:708286] C======= 02019-06-20 01:40:25.050391+0800 xxtest[2938:708286]2019-06-20 01:40:25.050487+0800 xxtest[2938:708288] A======= 12019-06-20 01:40:25.050588+0800 xxtest[2938:708285] B======= 12019-06-20 01:40:25.050684+0800 xxtest[2938:708286] C======= 12019-06-20 01:40:25.050782+0800 xxtest[2938:708286]2019-06-20 01:40:25.050881+0800 xxtest[2938:708288] A======= 22019-06-20 01:40:25.050982+0800 xxtest[2938:708285] B======= 22019-06-20 01:40:25.051442+0800 xxtest[2938:708286] C======= 22019-06-20 01:40:25.051695+0800 xxtest[2938:708286]2019-06-20 01:40:25.051928+0800 xxtest[2938:708288] A======= 32019-06-20 01:40:25.052162+0800 xxtest[2938:708285] B======= 32019-06-20 01:40:25.052414+0800 xxtest[2938:708286] C======= 32019-06-20 01:40:25.052841+0800 xxtest[2938:708286]2019-06-20 01:40:25.053073+0800 xxtest[2938:708288] A======= 42019-06-20 01:40:25.053340+0800 xxtest[2938:708285] B======= 42019-06-20 01:40:25.053617+0800 xxtest[2938:708286] C======= 42019-06-20 01:40:25.053843+0800 xxtest[2938:708286]2019-06-20 01:40:25.054094+0800 xxtest[2938:708288] A======= 52019-06-20 01:40:25.055448+0800 xxtest[2938:708285] B======= 52019-06-20 01:40:25.055584+0800 xxtest[2938:708286] C======= 52019-06-20 01:40:25.055757+0800 xxtest[2938:708286]2019-06-20 01:40:25.055888+0800 xxtest[2938:708288] A======= 62019-06-20 01:40:25.056116+0800 xxtest[2938:708285] B======= 62019-06-20 01:40:25.056455+0800 xxtest[2938:708286] C======= 62019-06-20 01:40:25.056871+0800 xxtest[2938:708286]2019-06-20 01:40:25.057176+0800 xxtest[2938:708288] A======= 72019-06-20 01:40:25.057484+0800 xxtest[2938:708285] B======= 72019-06-20 01:40:25.057789+0800 xxtest[2938:708286] C======= 72019-06-20 01:40:25.058204+0800 xxtest[2938:708286]2019-06-20 01:40:25.058479+0800 xxtest[2938:708288] A======= 82019-06-20 01:40:25.058947+0800 xxtest[2938:708285] B======= 82019-06-20 01:40:25.059211+0800 xxtest[2938:708286] C======= 82019-06-20 01:40:25.059469+0800 xxtest[2938:708286]2019-06-20 01:40:25.059701+0800 xxtest[2938:708288] A======= 92019-06-20 01:40:25.060025+0800 xxtest[2938:708285] B======= 92019-06-20 01:40:25.060360+0800 xxtest[2938:708286] C======= 92019-06-20 01:40:25.060585+0800 xxtest[2938:708286]
