1、同步串行队列
/**
不会创建新的线程,队列中的任务遵循FIFO
*/
- (void)syncSerial{
NSLog(@">>>>>>>>start<<<<<<<<<<<<");
dispatch_queue_t queue = dispatch_queue_create("downLoading", DISPATCH_QUEUE_SERIAL);
dispatch_sync(queue, ^{
for (NSInteger i = 0; i < 100; i ++) {
NSLog(@"download1:%zd---%@",i,[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{
for (NSInteger i = 0; i < 100; i ++) {
NSLog(@"download2:%zd---%@",i,[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{
for (NSInteger i = 0; i < 100; i ++) {
NSLog(@"download3:%zd---%@",i,[NSThread currentThread]);
}
});
NSLog(@">>>>>>>>end<<<<<<<<<<<<");
}
2、同步并行队列
/**
不会创建新的线程,队列中的任务遵循FIFO
*/
- (void)syncConCurrent {
NSLog(@">>>>>>>>Start<<<<<<<<<<<<");
dispatch_queue_t queue = dispatch_queue_create("downLoading", DISPATCH_QUEUE_CONCURRENT);
dispatch_sync(queue, ^{
for (NSInteger i = 0; i < 100; i ++) {
NSLog(@"download1:%zd---%@",i,[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{
for (NSInteger i = 0; i < 100; i ++) {
NSLog(@"download2:%zd---%@",i,[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{
for (NSInteger i = 0; i < 100; i ++) {
NSLog(@"download3:%zd---%@",i,[NSThread currentThread]);
}
});
NSLog(@">>>>>>>>end<<<<<<<<<<<<");
}
3、异步串行队列
/**
开启一条新线程
队列中的任务遵循FIFO执行
*/
- (void)ayncSerial {
NSLog(@">>>>>>>>start<<<<<<<<<<<<");
dispatch_queue_t queue = dispatch_queue_create("downLoading", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
for (NSInteger i = 0; i < 100; i ++) {
NSLog(@"download1:%zd---%@",i,[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (NSInteger i = 0; i < 100; i ++) {
NSLog(@"download2:%zd---%@",i,[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (NSInteger i = 0; i < 100; i ++) {
NSLog(@"download3:%zd---%@",i,[NSThread currentThread]);
}
});
NSLog(@">>>>>>>>end<<<<<<<<<<<<");
}
4、异步并行队列
/**
异步并行队列
开启多条新线程,任务并发执行
获取全局队列dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
* - DISPATCH_QUEUE_PRIORITY_HIGH:
* - DISPATCH_QUEUE_PRIORITY_DEFAULT:
* - DISPATCH_QUEUE_PRIORITY_LOW:
* - DISPATCH_QUEUE_PRIORITY_BACKGROUND:
第一个参数:队列优先级(2:HIGH 0:DEFAUL -2:LOW INT16_MIN:BACKGROUND:)
第二个参数:暂时未用到(Reserved for future use)
*/
- (void)ayncConCurrent {
NSLog(@">>>>>>>>Start<<<<<<<<<<<<");
//创建方式一:获取全局并发队列
// dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
//创建方式二:
dispatch_queue_t queue = dispatch_queue_create("downLoading", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
for (NSInteger i = 0; i < 100; i ++) {
NSLog(@"download1:%zd---%@",i,[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (NSInteger i = 0; i < 100; i ++) {
NSLog(@"download2:%zd---%@",i,[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (NSInteger i = 0; i < 100; i ++) {
NSLog(@"download3:%zd---%@",i,[NSThread currentThread]);
}
});
NSLog(@">>>>>>>>end<<<<<<<<<<<<");
}
5、死锁
主线程中创建同步串行队列
- (void)syncLock {
NSLog(@">>>>>>>>Start<<<<<<<<<<<<");
dispatch_queue_t queue = dispatch_get_main_queue() ;
dispatch_sync(queue, ^{
for (NSInteger i = 0; i < 100; i ++) {
NSLog(@"download1:%zd---%@",i,[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{
for (NSInteger i = 0; i < 100; i ++) {
NSLog(@"download2:%zd---%@",i,[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{
for (NSInteger i = 0; i < 100; i ++) {
NSLog(@"download3:%zd---%@",i,[NSThread currentThread]);
}
});
NSLog(@">>>>>>>>end<<<<<<<<<<<<");
}
主线程中直接调用该方法会导致死锁
[self syncLock]
原因:dispatch_sync会阻塞当前线程,然后把block中的任务放到指定的线程中执行,但此时主线程被阻塞,所以block中的任务始终不能完成,导致死锁现象。
解决方法:在主线程中开启新的线程
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self syncLock];
});
死锁.jpg
网友评论