多线程在开发过程中的应用。有些业务逻辑的需要,需要我们使用到多线程。今天就常用的业务模式进行一下简单的说明。
-
串行队列同步执行;
-
串行队列异步执行;
-
并行队列同步执行;
-
并行队列异步执行;
-
主队列异步执行;
-
主队列同步执行;
-
子线程下载主线程刷新;
-
队列组的使用;
-
Thread;
-
operation;
-
1、串行队列同步执行
串行队列同步执行 (在当前线程中顺序执行) 用于前后有依赖关系的处理 用的都是一条线程(在主线程中),不会开辟新线程。注意在主线程中耗时操作会造成主线程阻塞#
dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);//创建串行队列
dispatch_sync(serialQueue, ^{
DebugLog(@"串行队列同步执行- 1 == %@",[NSThread currentThread]);
});
dispatch_sync(serialQueue, ^{
DebugLog(@"串行队列同步执行- 2 == %@",[NSThread currentThread]);
});
dispatch_sync(serialQueue, ^{
for (int i = 0; i < 3; i++) {
DebugLog(@"串行队列同步执行- 3 == %@",[NSThread currentThread]);
}
});
//串行队列同步执行完以后走下面的代码(回到主线程)
[SVProgressHUD showInfoWithStatus:@"串行队列同步执行执行完毕"];
DebugLog(@"串行队列同步执行执行完毕 == %@",[NSThread currentThread]);
看一下执行顺序##
GCDLearnViewController.m:102
串行队列同步执行- 1 == <NSThread: 0x61800006fe40>{number = 1, name = main}
GCDLearnViewController.m:105
串行队列同步执行- 2 == <NSThread: 0x61800006fe40>{number = 1, name = main}
GCDLearnViewController.m:110
串行队列同步执行- 3 == <NSThread: 0x61800006fe40>{number = 1, name = main}
GCDLearnViewController.m:110
串行队列同步执行- 3 == <NSThread: 0x61800006fe40>{number = 1, name = main}
GCDLearnViewController.m:110
串行队列同步执行- 3 == <NSThread: 0x61800006fe40>{number = 1, name = main}
GCDLearnViewController.m:115
串行队列同步执行执行完毕 == <NSThread: 0x61800006fe40>{number = 1, name = main}
- 2、串行队列异步执行
串行队列异步执行 (在当前线程中顺序执行) 用于前后有依赖关系的处理 用的都是一条线程(在子线程中),耗时操作和有依赖关系的处理,可以用此方法#
dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
dispatch_async(serialQueue, ^{
DebugLog(@"串行队列异步执行 - 1 == %@",[NSThread currentThread]);
});
dispatch_async(serialQueue, ^{
for (int i = 0; i < 3; i++) {
DebugLog(@"串行队列异步执行 - 2 == %@",[NSThread currentThread]);
}
});
dispatch_async(serialQueue, ^{
for (int i = 0; i < 3; i++) {
DebugLog(@"串行队列异步执行 - 3 == %@",[NSThread currentThread]);
}
dispatch_async(dispatch_get_main_queue(), ^{//串行队列异步执行执行完毕 回到主线程
[SVProgressHUD showInfoWithStatus:@"串行队列异步执行执行完毕"];
DebugLog(@"串行队列异步执行执行完毕 == %@",[NSThread currentThread]);
});
});
看一下执行顺序##
GCDLearnViewController.m:122
串行队列异步执行 - 1 == <NSThread: 0x6180000755c0>{number = 5, name = (null)}
GCDLearnViewController.m:127
串行队列异步执行 - 2 == <NSThread: 0x6180000755c0>{number = 5, name = (null)}
GCDLearnViewController.m:127
串行队列异步执行 - 2 == <NSThread: 0x6180000755c0>{number = 5, name = (null)}
GCDLearnViewController.m:127
串行队列异步执行 - 2 == <NSThread: 0x6180000755c0>{number = 5, name = (null)}
GCDLearnViewController.m:133
串行队列异步执行 - 3 == <NSThread: 0x6180000755c0>{number = 5, name = (null)}
GCDLearnViewController.m:133
串行队列异步执行 - 3 == <NSThread: 0x6180000755c0>{number = 5, name = (null)}
GCDLearnViewController.m:133
串行队列异步执行 - 3 == <NSThread: 0x6180000755c0>{number = 5, name = (null)}
GCDLearnViewController.m:138
串行队列异步执行执行完毕 == <NSThread: 0x61800006fe40>{number = 1, name = main}
- 3、并行队列同步执行
并行队列同步执行 (number = 1, name = main) 在主线程中顺序执行;
dispatch_queue_t ConcurrentQueue = dispatch_queue_create("Concurrent", DISPATCH_QUEUE_CONCURRENT);
dispatch_sync(ConcurrentQueue, ^{
DebugLog(@"并行队列同步执行 - 1 == %@",[NSThread currentThread]);
});
dispatch_sync(ConcurrentQueue, ^{
for (int i = 0; i < 3; i++) {
DebugLog(@"并行队列同步执行 - 2 == %@",[NSThread currentThread]);
}
});
dispatch_sync(ConcurrentQueue, ^{
DebugLog(@"并行队列同步执行 - 3 == %@",[NSThread currentThread]);
});
看一下执行顺序##
GCDLearnViewController.m:147
并行队列同步执行 - 1 == <NSThread: 0x61800006fe40>{number = 1, name = main}
GCDLearnViewController.m:152
并行队列同步执行 - 2 == <NSThread: 0x61800006fe40>{number = 1, name = main}
GCDLearnViewController.m:152
并行队列同步执行 - 2 == <NSThread: 0x61800006fe40>{number = 1, name = main}
GCDLearnViewController.m:152
并行队列同步执行 - 2 == <NSThread: 0x61800006fe40>{number = 1, name = main}
GCDLearnViewController.m:156
并行队列同步执行 - 3 == <NSThread: 0x61800006fe40>{number = 1, name = main}
- 4、并行队列异步执行
并行队列异步执行 开辟新线程 无序执行#
dispatch_queue_t ConcurrentQueue = dispatch_queue_create("Concurrent", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(ConcurrentQueue, ^{
DebugLog(@"并行队列异步执行 - 1 == %@",[NSThread currentThread]);
});
dispatch_async(ConcurrentQueue, ^{
for (int i = 0; i < 3; i++) {
DebugLog(@"并行队列异步执行 - 2 == %@",[NSThread currentThread]);
}
});
dispatch_async(ConcurrentQueue, ^{
DebugLog(@"并行队列异步执行 - 3 == %@",[NSThread currentThread]);
});
看一下执行顺序##
GCDLearnViewController.m:164
并行队列异步执行 - 1 == <NSThread: 0x6180000755c0>{number = 5, name = (null)}
GCDLearnViewController.m:169
并行队列异步执行 - 2 == <NSThread: 0x61800007bc40>{number = 6, name = (null)}
GCDLearnViewController.m:173
并行队列异步执行 - 3 == <NSThread: 0x608000076440>{number = 7, name = (null)}
GCDLearnViewController.m:169
并行队列异步执行 - 2 == <NSThread: 0x61800007bc40>{number = 6, name = (null)}
GCDLearnViewController.m:169
并行队列异步执行 - 2 == <NSThread: 0x61800007bc40>{number = 6, name = (null)}
- 5、主队列异步执行
主队列异步执行 ({number = 1, name = main}) 在主线程中顺序执行#
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_async(mainQueue, ^{
DebugLog(@"主队列异步执行 - 1 == %@",[NSThread currentThread]);
});
dispatch_async(mainQueue, ^{
for (int i = 0; i < 3; i++) {
DebugLog(@"主队列异步执行 - 2 == %@",[NSThread currentThread]);
}
});
dispatch_async(mainQueue, ^{
DebugLog(@"主队列异步执行 - 3 == %@",[NSThread currentThread]);
});
看一下执行顺序##
GCDLearnViewController.m:181
主队列异步执行 - 1 == <NSThread: 0x61800006fe40>{number = 1, name = main}
GCDLearnViewController.m:186
主队列异步执行 - 2 == <NSThread: 0x61800006fe40>{number = 1, name = main}
GCDLearnViewController.m:186
主队列异步执行 - 2 == <NSThread: 0x61800006fe40>{number = 1, name = main}
GCDLearnViewController.m:186
主队列异步执行 - 2 == <NSThread: 0x61800006fe40>{number = 1, name = main}
GCDLearnViewController.m:190
主队列异步执行 - 3 == <NSThread: 0x61800006fe40>{number = 1, name = main}
- 6、主队列同步执行
主队列同步执行 会造成主线程卡死#
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_async(mainQueue, ^{
DebugLog(@"主队列同步执行 - 1 == %@",[NSThread currentThread]);
});
dispatch_async(mainQueue, ^{
for (int i = 0; i < 3; i++) {
DebugLog(@"主队列同步执行 - 2 == %@",[NSThread currentThread]);
}
});
dispatch_async(mainQueue, ^{
DebugLog(@"主队列同步执行 - 3 == %@",[NSThread currentThread]);
});
看一下执行顺序##
GCDLearnViewController.m:199
主队列同步执行 - 1 == <NSThread: 0x61800006fe40>{number = 1, name = main}
GCDLearnViewController.m:204
主队列同步执行 - 2 == <NSThread: 0x61800006fe40>{number = 1, name = main}
GCDLearnViewController.m:204
主队列同步执行 - 2 == <NSThread: 0x61800006fe40>{number = 1, name = main}
GCDLearnViewController.m:204
主队列同步执行 - 2 == <NSThread: 0x61800006fe40>{number = 1, name = main}
GCDLearnViewController.m:208
主队列同步执行 - 3 == <NSThread: 0x61800006fe40>{number = 1, name = main}
- 7、子线程下载主线程刷新
dispatch_async(dispatch_get_global_queue(0, 0), ^{
for (int i = 0; i < 3; i++) {
DebugLog(@"下载操作");
}
dispatch_async(dispatch_get_main_queue(), ^{//这个也是子线程和主线程间的一种通信
[SVProgressHUD showInfoWithStatus:@"下载完成,刷新主线程"];
DebugLog(@"下载完成,刷新主线程");
});
});
看一下执行顺序##
GCDLearnViewController.m:216
下载操作
GCDLearnViewController.m:216
下载操作
GCDLearnViewController.m:216
下载操作
GCDLearnViewController.m:220
下载完成,刷新主线程
- 8、队列组的使用
全部处理结束后刷新主线程#
dispatch_group_t group = dispatch_group_create();//队列组
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);//全局并发队列
dispatch_group_async(group, queue, ^{//异步操作1
for (int i = 0; i < 3; i ++) {
DebugLog(@"异步操作1");
}
});
dispatch_group_async(group, queue, ^{//异步操作2
for (int i = 0; i < 3; i ++) {
DebugLog(@"异步操作2");
}
});
dispatch_group_async(group, queue, ^{//异步操作3
for (int i = 0; i < 3; i ++) {
DebugLog(@"异步操作3");
}
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{//全部处理结束后刷新主线程
[SVProgressHUD showInfoWithStatus:@"异步操作完成,回到主线程"];
DebugLog(@"异步操作完成,回到主线程");
});
看一下执行顺序##
GCDLearnViewController.m:231
异步操作1
GCDLearnViewController.m:231
异步操作1
GCDLearnViewController.m:231
异步操作1
GCDLearnViewController.m:236
异步操作2
GCDLearnViewController.m:241
异步操作3
GCDLearnViewController.m:236
异步操作2
GCDLearnViewController.m:241
异步操作3
GCDLearnViewController.m:236
异步操作2
GCDLearnViewController.m:241
异步操作3
GCDLearnViewController.m:246
异步操作完成,回到主线程
- 9、Thread
NSThread *thread01 = [[NSThread alloc] initWithTarget:self selector:@selector(thread1) object:nil];
thread01.name = @"北京售票窗口";
[thread01 start];//需要启动
NSThread *thread02 = [[NSThread alloc] initWithTarget:self selector:@selector(thread1) object:nil];
thread02.name = @"广州售票窗口";
[thread02 start];//需要启动
while (1) {
@synchronized (self) {//加锁,防止资源争夺出现问题
if (ticketCont > 0) {
ticketCont--;
DebugLog(@"%@",[NSString stringWithFormat:@"剩余票数:%d 窗口:%@", ticketCont, [NSThread currentThread].name]);
[NSThread sleepForTimeInterval:.2f];
}
else{
if ([[NSThread currentThread] isCancelled]) {
break;
}
else{
DebugLog(@"票卖完了 %@",[NSThread currentThread]);
[[NSThread currentThread] cancel];
}
}
}
}
看一下打印##
GCDLearnViewController.m:325
剩余票数:9 窗口:北京售票窗口
GCDLearnViewController.m:325
剩余票数:8 窗口:广州售票窗口
GCDLearnViewController.m:325
剩余票数:7 窗口:北京售票窗口
GCDLearnViewController.m:325
剩余票数:6 窗口:广州售票窗口
GCDLearnViewController.m:325
剩余票数:5 窗口:北京售票窗口
GCDLearnViewController.m:325
剩余票数:4 窗口:广州售票窗口
GCDLearnViewController.m:325
剩余票数:3 窗口:北京售票窗口
GCDLearnViewController.m:325
剩余票数:2 窗口:广州售票窗口
GCDLearnViewController.m:325
剩余票数:1 窗口:北京售票窗口
GCDLearnViewController.m:325
剩余票数:0 窗口:广州售票窗口
GCDLearnViewController.m:333
票卖完了 <NSThread: 0x60000007e640>{number = 8, name = 北京售票窗口}
GCDLearnViewController.m:333
票卖完了 <NSThread: 0x610000269cc0>{number = 9, name = 广州售票窗口}
- 10、Operation
NSInvocationOperation *op1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(operation1:) object:nil];
op1.name = @"北京售票窗口";
NSInvocationOperation *op2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(operation1:) object:nil];
op2.name = @"广州售票窗口";
//如果不添加到NSOperationQueue里面就不会开启线程,会在当前线程中执行
operationQueue = [[NSOperationQueue alloc] init];//默认是并发的,如果想实现串行, 那么就设置队列的maxConcurrentOperationCount = 1
[operationQueue addOperation:op1];
[operationQueue addOperation:op2];
while (1) {
@synchronized (self) {//加锁,防止资源争夺出现问题
if (ticketCont > 0) {
ticketCont--;
DebugLog(@"%@",[NSString stringWithFormat:@"剩余票数:%d 窗口:%@ == %@", ticketCont, op.name,[NSThread currentThread]]);
}
else{
DebugLog(@"票卖完了 %@",[NSThread currentThread]);
break;
}
}
}
看一下打印##
GCDLearnViewController.m:352
剩余票数:9 窗口:(null) == <NSThread: 0x61800007ea00>{number = 11, name = (null)}
GCDLearnViewController.m:352
剩余票数:8 窗口:(null) == <NSThread: 0x6180000755c0>{number = 5, name = (null)}
GCDLearnViewController.m:352
剩余票数:7 窗口:(null) == <NSThread: 0x61800007ea00>{number = 11, name = (null)}
GCDLearnViewController.m:352
剩余票数:6 窗口:(null) == <NSThread: 0x6180000755c0>{number = 5, name = (null)}
GCDLearnViewController.m:352
剩余票数:5 窗口:(null) == <NSThread: 0x61800007ea00>{number = 11, name = (null)}
GCDLearnViewController.m:352
剩余票数:4 窗口:(null) == <NSThread: 0x6180000755c0>{number = 5, name = (null)}
GCDLearnViewController.m:352
剩余票数:3 窗口:(null) == <NSThread: 0x61800007ea00>{number = 11, name = (null)}
GCDLearnViewController.m:352
剩余票数:2 窗口:(null) == <NSThread: 0x6180000755c0>{number = 5, name = (null)}
GCDLearnViewController.m:352
剩余票数:1 窗口:(null) == <NSThread: 0x61800007ea00>{number = 11, name = (null)}
GCDLearnViewController.m:352
剩余票数:0 窗口:(null) == <NSThread: 0x6180000755c0>{number = 5, name = (null)}
GCDLearnViewController.m:355
票卖完了 <NSThread: 0x61800007ea00>{number = 11, name = (null)}
GCDLearnViewController.m:355
票卖完了 <NSThread: 0x6180000755c0>{number = 5, name = (null)}
网友评论