标签:主线程 串行 字符 标记 服务器端 相互 bar 手动 set
1.线程安全出现条件:多个线程访问更改同一个变量
2.OC在定义属性时有nonatomic和atomic两种选择
iOS开发建议:
GCD的两个核心
将任务添加到队列
同步和异步的区别
队列的类型
dispatch_queue_t concurrentQueue = dispatch_queue_create(
"concurrentQueue", //传递的字符串参数来标记线程
DISPATCH_QUEUE_CONCURRENT); //参数包括DISPATCH_QUEUE_SERIAL (串行队列)
DISPATCH_QUEUE_CONCURRENT (并行队列)
小结:
并发队列
dispatch_get_global_queue(
dispatch_queue_priority_t priority, //队列的优先级
unsigned long flags); //此参数暂时无用,用0即可
串行队列
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_async(mainQueue, ^{
NSLog(@"1----%@", [NSThread currentThread]);
});
dispatch_async(mainQueue, ^{
NSLog(@"2----%@", [NSThread currentThread]);
});
dispatch_async(mainQueue, ^{
NSLog(@"3----%@", [NSThread currentThread]);
});
dispatch_async(mainQueue, ^{
NSLog(@"4----%@", [NSThread currentThread]);
});
dispatch_async(mainQueue, ^{
NSLog(@"5----%@", [NSThread currentThread]);
});
- (void)lock
{
dispatch_queue_t mainQueue = dispatch_get_main_queue();
NSLog(@"开启任务");
dispatch_sync(mainQueue, ^{
NSLog(@"1----%@", [NSThread currentThread]);
});
dispatch_sync(mainQueue, ^{
NSLog(@"2----%@", [NSThread currentThread]);
});
dispatch_sync(mainQueue, ^{
NSLog(@"3----%@", [NSThread currentThread]);
});
dispatch_sync(mainQueue, ^{
NSLog(@"4----%@", [NSThread currentThread]);
});
dispatch_sync(mainQueue, ^{
NSLog(@"5----%@", [NSThread currentThread]);
});
}
注:线程死锁锁的到底是什么
分析在主线程调用下面代码是否会产生死锁
- (void)lock
{
dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
NSLog(@"当前线程----%@", [NSThread currentThread]);
dispatch_sync(serialQueue, ^{
NSLog(@"1----%@", [NSThread currentThread]);
});
dispatch_sync(serialQueue, ^{
NSLog(@"2----%@", [NSThread currentThread]);
});
dispatch_sync(serialQueue, ^{
NSLog(@"3----%@", [NSThread currentThread]);
});
dispatch_sync(serialQueue, ^{
NSLog(@"4----%@", [NSThread currentThread]);
});
dispatch_sync(serialQueue, ^{
NSLog(@"5----%@", [NSThread currentThread]);
});
NSLog(@"end");
}
在上面的代码调用方法在主线程,而且代码为同步执行,运行输出结果也在主线程,但在实际的运行中并没有出现死锁的现象,这是因为代码中lock所在的队列和lock中添加的任务队列并不是同一个队列,所以并不会出现任务相互等待的现象。换句话说上面描述的线程死锁是因为同一队列中任务的相互等待而造成的。
线程运行结果
dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
NSLog(@"1.当前线程----%@", [NSThread currentThread]);
dispatch_sync(serialQueue, ^{
NSLog(@"2----%@", [NSThread currentThread]);
});
dispatch_sync(serialQueue, ^{
NSLog(@"3----%@", [NSThread currentThread]);
});
dispatch_sync(serialQueue, ^{
NSLog(@"4----%@", [NSThread currentThread]);
});
dispatch_sync(serialQueue, ^{
NSLog(@"5----%@", [NSThread currentThread]);
});
dispatch_sync(serialQueue, ^{
NSLog(@"6----%@", [NSThread currentThread]);
});
NSLog(@"end");
因为均为同步函数,输出依次执行, 根据异步执行的特点不难分析出异步执行的结果为1、end、2、3、4、5;
dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
NSLog(@"1.当前线程----%@", [NSThread currentThread]);
dispatch_async(serialQueue, ^{
NSLog(@"2----%@", [NSThread currentThread]);
});
dispatch_async(serialQueue, ^{
NSLog(@"3----%@", [NSThread currentThread]);
});
dispatch_sync(serialQueue, ^{
NSLog(@"4----%@", [NSThread currentThread]);
});
dispatch_async(serialQueue, ^{
NSLog(@"5----%@", [NSThread currentThread]);
});
dispatch_async(serialQueue, ^{
NSLog(@"6----%@", [NSThread currentThread]);
});
NSLog(@"end");
在上面的代码中2、3、5、6任务为异步执行,4的任务为同步执行,输出结果为1、2、3、4、end、5、6;并且其执行的线程也会发生变化。这是由于在方法执行中首先将2、3、4函数添加到队列中,但是4方法为同步执行方法所以其会等待队列中的任务执行完毕后再往下执行,并且线程的同步执行任务并不会开启新的线程,所以4方法回到主线程中执行,2、3、5、6为异步执行,与4方法不在同一线程。5、6为异步执行,添加到队列后并不会立即执行,所以输出在end之后。
栅栏函数
dispatch_queue_t queue = dispatch_queue_create("concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
NSLog(@"1--- %@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"2--- %@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"3--- %@", [NSThread currentThread]);
});
//栅栏函数
dispatch_barrier_async(queue, ^{
NSLog(@"------上面的已经执行完毕------");
});
dispatch_async(queue, ^{
NSLog(@"4--- %@", [NSThread currentThread]);
});
注:栅栏函数不适用于全局并发队列(dispatch_ globle_queue)
dispatch_ async_ f的使用
dispatch_ async_ f和dispatch_ async功能相同,只是调用方式不同,dispatch_ async_f的使用如下
dispatch_queue_t globleQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async_f(globleQueue, NULL, tast);
void tast(void *param)
{
NSLog(@"%s --- %@", __func__, [NSThread currentThread]);
}
标签:主线程 串行 字符 标记 服务器端 相互 bar 手动 set
原文地址:https://www.cnblogs.com/GoodmorningMr/p/9591927.html