1、以下代码结果会如何?
NSLog(@"1===========%@",[NSThread currentThread]);
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"2===========%@",[NSThread currentThread]);
});
NSLog(@"3===========%@",[NSThread currentThread]);
结果如下:
1==========={number = 1, name = main}
会造成死锁,主线程中【同步执行+主队列】,造成的互相等待。
2、写一个线程安全的购票代码
- (void)initTicketStatusSave {
NSLog(@"start_%@",[NSThread currentThread]);
semaphoreLock = dispatch_semaphore_create(1);
self.ticketSurplusCount = 6;
//北京售票口
dispatch_queue_t queueBJ = dispatch_queue_create("beijin", DISPATCH_QUEUE_SERIAL);
//上海售票口
dispatch_queue_t queueSH = dispatch_queue_create("shanghai",DISPATCH_QUEUE_SERIAL);
__weak typeof(self) weakSelf = self;
dispatch_async(queueBJ, ^{
[weakSelf saleTicketSafe];
});
dispatch_async(queueSH, ^{
[weakSelf saleTicketSafe];
});
}
- (void)saleTicketSafe {
while (1) {
dispatch_semaphore_wait(semaphoreLock, DISPATCH_TIME_FOREVER);
if (self.ticketSurplusCount > 0) {
self.ticketSurplusCount--;
NSLog(@"%@",[NSString stringWithFormat:@"剩余票数:%ld 窗口:%@",self.ticketSurplusCount,[NSThread currentThread]]);
[NSThread sleepForTimeInterval:0.2];
} else {
NSLog(@"所有火车票均已售完");
dispatch_semaphore_signal(semaphoreLock);
break;;
}
dispatch_semaphore_signal(semaphoreLock);
}
}
start_{number = 1, name = main}
剩余票数:5 窗口:{number = 5, name = (null)}
剩余票数:4 窗口:{number = 6, name = (null)}
剩余票数:3 窗口:{number = 5, name = (null)}
剩余票数:2 窗口:{number = 6, name = (null)}
剩余票数:1 窗口:{number = 5, name = (null)}
剩余票数:0 窗口:{number = 6, name = (null)}
所有火车票均已售完
所有火车票均已售完
3、请说出下列的打印顺序
- (void)GCDForTest {
dispatch_queue_t testQueue = dispatch_queue_create("testQueue", DISPATCH_QUEUE_SERIAL);
NSLog(@"1 ---- %@",[NSThread currentThread]);
dispatch_async(testQueue, ^{
[NSThread sleepForTimeInterval:1];
NSLog(@"2 ---- %@",[NSThread currentThread]);
});
NSLog(@"3 ---- %@",[NSThread currentThread]);
dispatch_sync(testQueue, ^{
[NSThread sleepForTimeInterval:1];
NSLog(@"4 ---- %@",[NSThread currentThread]);
});
NSLog(@"5 ---- %@",[NSThread currentThread]);
}
打印顺序为:1、3、2、4、5
,你说对了吗?
1 ---- {number = 1, name = main}
3 ---- {number = 1, name = main}
2 ---- {number = 5, name = (null)}
4 ---- {number = 1, name = main}
5 ---- {number = 1, name = main}
在任务1
执行完后,将任务2
添加至串行队列中,由于是异步的,所以不会堵塞线程。继续往下执行任务3
,然后将任务4
添加至串行队列,任务2
和任务4
此时在同一个串行队列,根据队列的先进先出原则,同步执行的任务4
必须得等任务2
执行完后才能执行,不然会堵塞线程。
4、下面的代码会如何打印
- (void)demoForSelector {
NSLog(@"1 ---- %@",[NSThread currentThread]);
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSLog(@"2 ---- %@",[NSThread currentThread]);
[self performSelector:@selector(testForSelector:) withObject:@"performSelector" afterDelay:1];
});
NSLog(@"3 ---- %@",[NSThread currentThread]);
}
- (void)testForSelector:(NSString *)msg {
NSLog(@"%@ ---- %@",msg,[NSThread currentThread]);
}
1 ---- {number = 1, name = main}
3 ---- {number = 1, name = main}
2 ---- {number = 5, name = (null)}
可以看到,testForSelector:
方法没走,为什么?
是因为在GCD异步全局并发队列的子线程里,默认是没有开启对应的RunLoop的,所以这个方法就会失效。
有3中方式可以让这个方法起作用:
- 将
dispatch_get_global_queue
换成dispatch_get_main_queue
- 将
dispatch_async
换成dispatch_async
- 将
testForSelector:
方法添加到RunLoop里
NSRunLoop *loop = [NSRunLoop currentRunLoop];
[self performSelector:@selector(testForSelector:) withObject:@"performSelector" afterDelay:0.1];
[loop run];
5、怎么用GCD实现多读单写?
多读单写:可以多个读者同时读取数据,但是在读的时候不能去写入数据。写的时候,只能单个写,不能有其他写者同时去写。
- (void)多读单写 {
dispatch_queue_t queue = dispatch_queue_create("duodudanxie", DISPATCH_QUEUE_CONCURRENT);
//读
for (int i = 0; i < 5; i++) {
dispatch_async(queue, ^{
NSLog(@"%d ---- %@",i,[NSThread currentThread]);
});
}
//写
dispatch_barrier_sync(queue, ^{
NSLog(@"barrier ---- %@",[NSThread currentThread]);
});
//读
for (int i = 10; i < 5; i++) {
dispatch_async(queue, ^{
NSLog(@"%d ---- %@",i,[NSThread currentThread]);
});
}
}
0 ---- {number = 5, name = (null)}
1 ---- {number = 6, name = (null)}
3 ---- {number = 7, name = (null)}
2 ---- {number = 3, name = (null)}
4 ---- {number = 8, name = (null)}
barrier ---- {number = 1, name = main}
10 ---- {number = 8, name = (null)}
13 ---- {number = 4, name = (null)}
11 ---- {number = 3, name = (null)}
12 ---- {number = 7, name = (null)}
14 ---- {number = 6, name = (null)}
6、在多个网络请求完成后才去刷新UI界面
在n个耗时并发任务都完成后,再去执行接下来的任务。
dispatch_group_async
- (void)demoForGroup {
dispatch_queue_t concurrentQueue = dispatch_queue_create("groupqueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group = dispatch_group_create();
for (NSInteger i = 0; i < 5; i++) {
dispatch_group_async(group, concurrentQueue, ^{
[NSThread sleepForTimeInterval:1];
NSLog(@"网络请求:%ld ---- %@",i,[NSThread currentThread]);
});
}
dispatch_group_notify(group, concurrentQueue, ^{
NSLog(@"刷新UI ---- %@",[NSThread currentThread]);
});
}
网络请求:2 ---- {number = 6, name = (null)}
网络请求:0 ---- {number = 5, name = (null)}
网络请求:3 ---- {number = 4, name = (null)}
网络请求:1 ---- {number = 7, name = (null)}
网络请求:4 ---- {number = 3, name = (null)}
刷新UI ---- {number = 3, name = (null)}
dispatch_barrier_sync
dispatch_queue_t concurrentQueue = dispatch_queue_create("groupqueue", DISPATCH_QUEUE_CONCURRENT);
for (NSInteger i = 0; i < 5; i++) {
dispatch_async(concurrentQueue, ^{
[NSThread sleepForTimeInterval:1];
NSLog(@"网络请求:%ld ---- %@",i,[NSThread currentThread]);
});
}
dispatch_barrier_sync(concurrentQueue, ^{
NSLog(@"刷新UI ---- %@",[NSThread currentThread]);
});
NSLog(@"end");
网络请求:3 ---- {number = 8, name = (null)}
网络请求:0 ---- {number = 7, name = (null)}
网络请求:4 ---- {number = 5, name = (null)}
网络请求:2 ---- {number = 6, name = (null)}
网络请求:1 ---- {number = 4, name = (null)}
刷新UI ---- {number = 1, name = main}
end
参考
2019 iOS面试题-----多线程相关
阿里、字节:一套高效的iOS面试题(八 - 多线程 GCD)