dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_async(mainQueue, ^{
NSLog(@"第一个任务,所在线程:%@, 是否是主线程:%d", [NSThread currentThread], [NSThread currentThread].isMainThread);
});
dispatch_async(mainQueue, ^{
NSLog(@"第二个任务,所在线程:%@, 是否是主线程:%d", [NSThread currentThread], [NSThread currentThread].isMainThread);
});
dispatch_async(mainQueue, ^{
NSLog(@"第三个任务,所在线程:%@, 是否是主线程:%d", [NSThread currentThread], [NSThread currentThread].isMainThread);
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(33 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"延迟3秒执行");
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 55ull * NSEC_PER_SEC), mainQueue, ^{
NSLog(@"延迟5秒执行");
});
dispatch_queue_t myQueue = dispatch_queue_create("com.wl.MyQueue", DISPATCH_QUEUE_SERIAL);
dispatch_async(myQueue, ^{
for (int i = 0; i < 10; i++) {
NSLog(@"第一个任务,所在线程:%@, 是否是主线程:%d-----%d", [NSThread currentThread], [NSThread currentThread].isMainThread,i);
}
});
dispatch_async(myQueue, ^{
for (int i = 10; i < 20; i++) {
NSLog(@"第二个任务,所在线程:%@, 是否是主线程:%d-----%d", [NSThread currentThread], [NSThread currentThread].isMainThread,i);
}
});
dispatch_async(myQueue, ^{
for (int i = 20; i < 30; i++) {
NSLog(@"第三个任务,所在线程:%@, 是否是主线程:%d-----%d", [NSThread currentThread], [NSThread currentThread].isMainThread,i);
}
});
dispatch_queue_t myQueue = dispatch_queue_create("com.lanou3g.ConcurrentQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(myQueue, ^{
for (int i = 0; i < 10; i++) {
NSLog(@"第一个任务,所在线程:%@, 是否是主线程:%d~~~~~~~~~~~%d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);
}
});
dispatch_async(myQueue, ^{
for (int i = 10; i < 20; i++) {
NSLog(@"第二个任务,所在线程:%@, 是否是主线程:%d~~~~~~~~~~~%d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);
}
});
dispatch_async(myQueue, ^{
for (int i = 20; i < 30; i++) {
NSLog(@"第三个任务,所在线程:%@, 是否是主线程:%d~~~~~~~~~~~%d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);
}
});
- (void)group
{
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t myQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_async(group, myQueue, ^{
for (int i = 0; i < 10; i++) {
NSLog(@"第一个任务,所在线程:%@, 是否是主线程:%d~~~~~~~~~~~%d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);
}
});
dispatch_group_async(group, myQueue, ^{
for (int i = 10; i < 20; i++) {
NSLog(@"第二个任务,所在线程:%@, 是否是主线程:%d~~~~~~~~~~~%d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);
}
});
dispatch_group_notify(group, myQueue, ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"当前的线程是: %@", [NSThread currentThread]);
NSLog(@"所有数据下载完成.可以去刷新UI了");
});
});
dispatch_group_async(group, myQueue, ^{
for (int i = 20; i < 30; i++) {
NSLog(@"第三个任务,所在线程:%@, 是否是主线程:%d~~~~~~~~~~~%d", [NSThread currentThread], [NSThread currentThread].isMainThread, i);
}
});
}
- (void)barrier
{
dispatch_queue_t queue = dispatch_queue_create("com.lanou3g.Barrier", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
NSLog(@"玩家一读取完成");
});
dispatch_async(queue, ^{
NSLog(@"玩家二读取完成");
});
dispatch_async(queue, ^{
NSLog(@"玩家三读取完成");
});
dispatch_barrier_async(queue, ^{
NSLog(@"等待其他玩家进入...");
});
dispatch_async(queue, ^{
NSLog(@"玩家一进入游戏");
});
dispatch_async(queue, ^{
NSLog(@"玩家二进入游戏");
});
dispatch_async(queue, ^{
NSLog(@"玩家三进入游戏");
});
dispatch_barrier_async(queue, ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"------%d", [NSThread currentThread].isMainThread);
NSLog(@"敌军即将在30秒后进入战场");
});
});
}
- (void)onceBlock
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@"第一滴血");
});
}