import “ViewController.h”
- (IBAction)Serial:(UIButton *)sender {
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_async(mainQueue, ^{
NSLog(@"第1个任务,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(mainQueue, ^{
NSLog(@"第2个任务,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(mainQueue, ^{
NSLog(@"第3个任务,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(mainQueue, ^{
NSLog(@"第4个任务,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(mainQueue, ^{
NSLog(@"第5个任务,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_queue_t mySerialQueue = dispatch_queue_create("com.lanou3g.GCD.mySerialQueue", DISPATCH_QUEUE_SERIAL);
dispatch_async(mySerialQueue, ^{
NSLog(@"第1个任务,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(mySerialQueue, ^{
NSLog(@"第2个任务,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(mySerialQueue, ^{
NSLog(@"第3个任务,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(mySerialQueue, ^{
NSLog(@"第4个任务,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(mySerialQueue, ^{
NSLog(@"第5个任务,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
}
- (IBAction)Concurrent:(UIButton *)sender {
dispatch_queue_t myConcurrentQueue = dispatch_queue_create("com.lanou3g.GCD.myConcurrentQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(myConcurrentQueue, ^{
NSLog(@"第1个任务,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(myConcurrentQueue, ^{
NSLog(@"第2个任务,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(myConcurrentQueue, ^{
NSLog(@"第3个任务,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(myConcurrentQueue, ^{
NSLog(@"第4个任务,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(myConcurrentQueue, ^{
NSLog(@"第5个任务,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(myConcurrentQueue, ^{
NSLog(@"第6个任务,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(myConcurrentQueue, ^{
NSLog(@"第7个任务,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(myConcurrentQueue, ^{
NSLog(@"第8个任务,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(myConcurrentQueue, ^{
NSLog(@"第9个任务,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(myConcurrentQueue, ^{
NSLog(@"第10个任务,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
}
- (IBAction)after:(UIButton *)sender {
double delayInSeconds = 3.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_queue_t myConcurrentQueue = dispatch_queue_create("com.lanou3g.GCD.myConcurrentQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_after(popTime, myConcurrentQueue, ^(void){
NSLog(@"world");
});
}
- (IBAction)group:(UIButton *)sender {
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t myConcurrentQueue = dispatch_queue_create("com.lanou3g.GCD.myConcurrentQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_async(group, myConcurrentQueue, ^{
NSLog(@"第1个任务,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_group_async(group, myConcurrentQueue, ^{
NSLog(@"第2个任务,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_group_async(group, myConcurrentQueue, ^{
NSLog(@"第3个任务,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_group_async(group, myConcurrentQueue, ^{
NSLog(@"第4个任务,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_group_notify(group, myConcurrentQueue, ^{
NSLog(@"group中的任务都执行完毕之后,执行此任务。所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_group_async(group, myConcurrentQueue, ^{
NSLog(@"第5个任务,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_group_async(group, myConcurrentQueue, ^{
NSLog(@"第6个任务,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
}
- (IBAction)barrier:(UIButton *)sender {
dispatch_queue_t myConcurrentQueue = dispatch_queue_create("com.lanou3g.GCD.myConcurrentQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(myConcurrentQueue, ^{
NSLog(@"读取一些数据,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(myConcurrentQueue, ^{
NSLog(@"读取另外一些数据,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(myConcurrentQueue, ^{
NSLog(@"读取这些数据,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(myConcurrentQueue, ^{
NSLog(@"读取那些数据,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_barrier_async(myConcurrentQueue, ^{
NSLog(@"写入某些数据,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(myConcurrentQueue, ^{
NSLog(@"读取XXX数据,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(myConcurrentQueue, ^{
NSLog(@"读取OOXX数据,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(myConcurrentQueue, ^{
NSLog(@"读取aaa数据,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
dispatch_async(myConcurrentQueue, ^{
NSLog(@"读取bbb数据,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
}
- (IBAction)apply:(UIButton *)sender {
NSArray *array = [NSArray arrayWithObjects:@"红楼梦",@"水浒传",@"三国演义",@"西游记", nil];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_apply([array count], queue, ^(size_t index) {
NSLog(@"%@所在线程%@,是否是主线程:%d",[array objectAtIndex:index],[NSThread currentThread],[[NSThread currentThread] isMainThread]);
});
}
- (IBAction)once:(UIButton *)sender {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@"只执行一次");
});
}
- (IBAction)syn:(id)sender {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSLog(@"读取s数据,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
for (int i = 0; i < 100; i++) {
NSLog(@"第一组循环 = %d",i);
}
});
dispatch_async(queue, ^{
NSLog(@"读取as数据,所在线程%@,是否是主线程:%d",[NSThread currentThread],[[NSThread currentThread] isMainThread]);
for (int i = 0; i < 10; i++) {
NSLog(@"第二组循环 = %d",i);
}
});
NSLog(@"haha");
}
- (IBAction)functionPoint:(id)sender {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async_f(queue, @"你好", function);
}
void function(void *context)
{
NSLog(@"%@ 所在线程%@,是否是主线程:%d",context,[NSThread currentThread],[[NSThread currentThread] isMainThread]);
}