“这是我参与8月更文挑战的第5天,活动详情查看:8月更文挑战”
一.主队列同步
/**
主队列同步
不会开线程
*/
- (void)mainSyncTest{
NSLog(@"0");
// 等 死锁
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"1");
NSLog(@"%@",[NSThread currentThread]);
});
NSLog(@"2");
}
复制代码
运行打印结果
二.主队列异步
/**
主队列异步
不会开线程 顺序
*/
- (void)mainAsyncTest{
NSLog(@"0");
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"1");
NSLog(@"%@",[NSThread currentThread]);
});
NSLog(@"2");
}
复制代码
运行打印结果
三.全局队列同步
/**
全局同步
全局队列:一个并发队列 同步在主线程
*/
- (void)globalSyncTest{
for (int i = 0; i<10; i++) {
dispatch_sync(dispatch_get_global_queue(0, 0), ^{
NSLog(@"%d-%@",i,[NSThread currentThread]);
});
}
NSLog(@"hello queue");
}
复制代码
运行打印结果
四.全局列异步
/**
全局异步
全局队列:一个并发队列
*/
- (void)globalAsyncTest{
for (int i = 0; i<10; i++) {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSLog(@"%d-%@",i,[NSThread currentThread]);
});
}
NSLog(@"hello queue");
}
复制代码
运行打印结果
五.自定义串行队列同步
/**
串行同步队列 : FIFO: 先进先出 (在当前线程执行)
*/
- (void)serialSyncTest{
//1:创建串行队列
NSLog(@"hello");
dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_SERIAL);
for (int i = 0; i<10; i++) {
dispatch_sync(queue, ^{
NSLog(@"%d-%@",i,[NSThread currentThread]);
});
}
NSLog(@"queue");
}
复制代码
运行打印结果
六.自定义串行队列异步
/**
串行异步队列 (开启一条线程 任务一个接一个)
*/
- (void)serialAsyncTest{
//1:创建串行队列
NSLog(@"hello");
dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_SERIAL);
for (int i = 0; i<100; i++) {
dispatch_async(queue, ^{
NSLog(@"%d-%@",i,[NSThread currentThread]);
});
}
NSLog(@"queue");
}
复制代码
运行打印结果
七.自定义并发队列同步
/**
同步并发 : 堵塞 同步锁 队列 : resume supend 线程 操作, 队列挂起 任务能否执行 (不会开启线程 在当前线程执行)
*/
- (void)concurrentSyncTest{
NSLog(@"hello");
//1:创建并发队列
dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
for (int i = 0; i<10; i++) {
dispatch_sync(queue, ^{
NSLog(@"%d-%@",i,[NSThread currentThread]);
});
}
NSLog(@"queue");
}
复制代码
运行打印结果
八.自定义并发队列异步
/**
异步并发: 有了异步函数不一定开辟线程(开启线程 在当前线程执行)
*/
- (void)concurrentAsyncTest{
NSLog(@"hello");
//1:创建并发队列
dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
for (int i = 0; i<100; i++) {
dispatch_async(queue, ^{
NSLog(@"%d-%@",i,[NSThread currentThread]);
});
}
NSLog(@"queue");
}
复制代码
运行打印结果
九.并发队列-异步-同步
- (void)concurrentASync_Sync{
// 同步队列
dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"1");
// 异步函数
dispatch_async(queue, ^{
NSLog(@"2");
// 同步
dispatch_sync(queue, ^{
NSLog(@"3");
});
NSLog(@"4");
});
NSLog(@"5");
}
复制代码
运行结果打印
十.并发队列-异步-异步
- (void)concurrentASync_ASync{
dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"1");
// 耗时
dispatch_async(queue, ^{
NSLog(@"2");
dispatch_async(queue, ^{
NSLog(@"3");
});
NSLog(@"4");
});
NSLog(@"5");
}
复制代码
运行结果打印
十一.并发队列-同步-同步
- (void)concurrentSync_Sync{
// 同步队列
dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"1");
// 异步函数
dispatch_sync(queue, ^{
NSLog(@"2");
// 同步
dispatch_sync(queue, ^{
NSLog(@"3");
});
NSLog(@"4");
});
NSLog(@"5");
}
复制代码
运行结果打印
十二.并发队列-同步-异步
- (void)concurrentSync_ASync{
dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"1");
// 耗时
dispatch_sync(queue, ^{
NSLog(@"2");
dispatch_async(queue, ^{
NSLog(@"3");
});
NSLog(@"4");
});
NSLog(@"5");
}
复制代码
运行结果打印
十三.串行队列-异步-同步
- (void)serialAsync_Sync{
dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_SERIAL);
NSLog(@"1");
dispatch_async(queue, ^{
NSLog(@"2");
//死锁
dispatch_sync(queue, ^{
NSLog(@"3");
});
NSLog(@"4");
});
NSLog(@"5");
}
复制代码
运行结果打印
十四.串行队列-异步-异步
- (void)serialAsync_ASync{
dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_SERIAL);
NSLog(@"1");
dispatch_async(queue, ^{
NSLog(@"2");
dispatch_async(queue, ^{
NSLog(@"3");
});
NSLog(@"4");
});
NSLog(@"5");
}
复制代码
运行结果打印
十五.串行队列-同步-异步
dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_SERIAL);
NSLog(@"1");
dispatch_sync(queue, ^{
NSLog(@"2");
dispatch_async(queue, ^{
NSLog(@"3");
});
NSLog(@"4");
});
NSLog(@"5");
}
复制代码
运行结果打印
十六.串行队列-同步-同步
dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_SERIAL);
NSLog(@"1");
dispatch_sync(queue, ^{
NSLog(@"2");
//死锁
dispatch_sync(queue, ^{
NSLog(@"3");
});
NSLog(@"4");
});
NSLog(@"5");
}
}
复制代码
运行结果打印
十七.并发——dispatch_barrier_sync
-(void)concurrent_barrierSync
{
dispatch_queue_t concurrentQueue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
for (NSInteger i = 0; i < 10; i++) {
dispatch_sync(concurrentQueue, ^{
NSLog(@"%zd",i);
});
}
dispatch_barrier_sync(concurrentQueue, ^{
NSLog(@"barrier");
});
for (NSInteger i = 10; i < 20; i++) {
dispatch_sync(concurrentQueue, ^{
NSLog(@"%zd",i);
});
}
}
复制代码
运行打印结果
十八.并发——dispatch_barrier_async
-(void)concurrent_barrierASync
{
dispatch_queue_t concurrentQueue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
for (NSInteger i = 0; i < 10; i++) {
dispatch_sync(concurrentQueue, ^{
NSLog(@"%zd",i);
});
}
dispatch_barrier_async(concurrentQueue, ^{
NSLog(@"barrier");
});
for (NSInteger i = 10; i < 20; i++) {
dispatch_sync(concurrentQueue, ^{
NSLog(@"%zd",i);
});
}
}
复制代码
运行打印结果
十七.个别案例
1.面试题1
-(void)perform {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self performSelector:@selector(test) withObject:nil afterDelay:0];
});
}
-(void)test{
NSLog(@"hello word");
}
复制代码
不会打印hello word
这个方法要创建提交任务到runloop
上的,而gcd
底层创建的线程是默认没有开启对应runloop
的,所有这个方法就会失效。
以下4种解决方式:
- 1.将
dispatch_get_global_queue
改成dispatch_get_main_queue()
,由于主队列所在的主线程是默认开启了runloop
的 - 2.将
dispatch_async
改成dispatch_sync
,因为同步是在当前线程执行,那么如果当前线程是主线程,test
方法也是会去执行的 - 3.添加
runloop
线程保活
-(void)perform {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[[NSRunLoop currentRunLoop] addPort:[NSPort new] forMode:NSRunLoopCommonModes];
[self performSelector:@selector(test) withObject:nil afterDelay:0];
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
});
}
复制代码
- 4.将
[self performSelector:@selector(test) withObject:nil afterDelay:0];
改为[self performSelector:@selector(test) withObject:nil];
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END