//
// ViewController.m
// GCD-test
//
// Created by on 16/9/13.
// Copyright © 2016年 . All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor whiteColor];
//四种队列,八种操作方式
[self test1];
//代码只执行一次,常使用在单利
[self test2];
//代码延迟执行
[self test3];
//代码多次执行
[self test4];
//将任务放进一个组内进行控制
[self test5];
//任务执行到第几个时时,监听
[self test6];
}
//四种队列,八种操作方式
-(void)test1{
//主队列
dispatch_queue_t queue1=dispatch_get_main_queue();
//全局队列 可以理解为并行
dispatch_queue_t queue2=dispatch_get_global_queue(0, 0);
//创建一个队列名字为myqueue(第一个参数),为串行(第二个参数)
dispatch_queue_t queue3=dispatch_queue_create("myquwue", DISPATCH_QUEUE_SERIAL);
//创建一个队列名字为myqueue1(第一个参数),为并行(第二个参数)
dispatch_queue_t quqeue4=dispatch_queue_create("myquque1", DISPATCH_QUEUE_CONCURRENT);
//主队列异步操作
//顺序执行,不创建新线程
dispatch_async(queue1, ^{
NSLog(@"test1--%@",[NSThread currentThread]);
});
dispatch_async(queue1, ^{
NSLog(@"twst2--%@",[NSThread currentThread]);
});
dispatch_async(queue1, ^{
NSLog(@"twst3--%@",[NSThread currentThread]);
});
//主队列的同步操作,产生死锁
dispatch_sync(queue1, ^{
NSLog(@"test1--%@",[NSThread currentThread]);
});
//全局队列的异步操作
//创建新的线程,异步执行,执行随机
dispatch_async(queue2, ^{
NSLog(@"test1--%@",[NSThread currentThread]);
});
dispatch_async(queue2, ^{
NSLog(@"test2--%@",[NSThread currentThread]);
});
dispatch_async(queue2, ^{
NSLog(@"test3--%@",[NSThread currentThread]);
});
dispatch_async(queue2, ^{
NSLog(@"test4--%@",[NSThread currentThread]);
});
//全局队列的同步操作
//不开启新的线程,顺序执行
dispatch_sync(queue2, ^{
NSLog(@"--test1--%@",[NSThread currentThread]);
});
dispatch_sync(queue2, ^{
NSLog(@"--test2--%@",[NSThread currentThread]);
});
dispatch_sync(queue2, ^{
NSLog(@"--test3--%@",[NSThread currentThread]);
});
//串行队列的异步操作
//创建一个新的线程,顺序执行
dispatch_async(queue3, ^{
NSLog(@"--test1--%@",[NSThread currentThread]);
});
dispatch_async(queue3, ^{
NSLog(@"--test2--%@",[NSThread currentThread]);
});
dispatch_async(queue3, ^{
NSLog(@"--test3--%@",[NSThread currentThread]);
});
dispatch_async(queue3, ^{
NSLog(@"--test4--%@",[NSThread currentThread]);
});
//串行队列的同步操作
//不创建新的线程,顺序执行
dispatch_sync(queue3, ^{
NSLog(@"----1----%@",[NSThread currentThread]);
});
dispatch_sync(queue3, ^{
NSLog(@"----2----%@",[NSThread currentThread]);
});
dispatch_sync(queue3, ^{
NSLog(@"----3----%@",[NSThread currentThread]);
});
//并行队列的异步操作
//创建多个线程,异步操作,随机执行
dispatch_async(quqeue4, ^{
NSLog(@"----1----%@",[NSThread currentThread]);
});
dispatch_async(quqeue4, ^{
NSLog(@"----2----%@",[NSThread currentThread]);
});
dispatch_async(quqeue4, ^{
NSLog(@"----3----%@",[NSThread currentThread]);
});
dispatch_async(quqeue4, ^{
NSLog(@"----4----%@",[NSThread currentThread]);
});
// 并行队列的同步操作
// 不创建新的线程,顺序执行
dispatch_sync(quqeue4, ^{
NSLog(@"----1----%@",[NSThread currentThread]);
});
dispatch_sync(quqeue4, ^{
NSLog(@"----2----%@",[NSThread currentThread]);
});
dispatch_sync(quqeue4, ^{
NSLog(@"----3----%@",[NSThread currentThread]);
});
dispatch_sync(quqeue4, ^{
NSLog(@"----4----%@",[NSThread currentThread]);
});
}
-(void)test2{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@"--1--");
});
}
-(void)test3{
//计时器
[NSTimer scheduledTimerWithTimeInterval:4.0 target:self selector:@selector(task) userInfo:nil repeats:NO];
//阻塞线程,不推荐使用
[NSThread sleepForTimeInterval:2.0];
[self task];
//新建一个线程,两秒后执行,在全局队列中执行,不影响主线程执行
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_global_queue(0, 0), ^{
[self task];
});
NSLog(@"=====");
}
-(void)test4{
//<#size_t iterations#> 要重复几次 <#dispatch_queue_t queue#> 在哪个队列中 <#^(size_t)block#> 带有参数的Block, index的作用是为了按执行的顺序区分各个Block
dispatch_apply(10, dispatch_get_global_queue(0, 0), ^(size_t index) {
NSLog(@"第%ld个,thread:%@",index,[NSThread currentThread]);
});
//nslog一定在最后输出,dispatch_apply 会等待所有的执行结束后才继续执行
NSLog(@"--------");
}
-(void)test5{
dispatch_queue_t queue= dispatch_get_global_queue(0, 0);
dispatch_group_t group=dispatch_group_create();
dispatch_group_async(group, queue, ^{
NSLog(@"----1----%@",[NSThread currentThread]);
});
dispatch_group_async(group, queue, ^{
NSLog(@"----2----%@",[NSThread currentThread]);
});
dispatch_group_async(group, queue, ^{
NSLog(@"----3----%@",[NSThread currentThread]);
});
//监听 group执行完输出
dispatch_group_notify(group, queue, ^{
NSLog(@"end");
});
}
-(void)test6{
dispatch_queue_t queue=dispatch_queue_create("hhjjj", 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_sync(queue, ^{
NSLog(@"-----%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"----4----%@",[NSThread currentThread]);
});
}
-(void)task{
NSLog(@"run%@",[NSThread currentThread]);
}
@end
// ViewController.m
// GCD-test
//
// Created by on 16/9/13.
// Copyright © 2016年 . All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor whiteColor];
//四种队列,八种操作方式
[self test1];
//代码只执行一次,常使用在单利
[self test2];
//代码延迟执行
[self test3];
//代码多次执行
[self test4];
//将任务放进一个组内进行控制
[self test5];
//任务执行到第几个时时,监听
[self test6];
}
//四种队列,八种操作方式
-(void)test1{
//主队列
dispatch_queue_t queue1=dispatch_get_main_queue();
//全局队列 可以理解为并行
dispatch_queue_t queue2=dispatch_get_global_queue(0, 0);
//创建一个队列名字为myqueue(第一个参数),为串行(第二个参数)
dispatch_queue_t queue3=dispatch_queue_create("myquwue", DISPATCH_QUEUE_SERIAL);
//创建一个队列名字为myqueue1(第一个参数),为并行(第二个参数)
dispatch_queue_t quqeue4=dispatch_queue_create("myquque1", DISPATCH_QUEUE_CONCURRENT);
//主队列异步操作
//顺序执行,不创建新线程
dispatch_async(queue1, ^{
NSLog(@"test1--%@",[NSThread currentThread]);
});
dispatch_async(queue1, ^{
NSLog(@"twst2--%@",[NSThread currentThread]);
});
dispatch_async(queue1, ^{
NSLog(@"twst3--%@",[NSThread currentThread]);
});
//主队列的同步操作,产生死锁
dispatch_sync(queue1, ^{
NSLog(@"test1--%@",[NSThread currentThread]);
});
//全局队列的异步操作
//创建新的线程,异步执行,执行随机
dispatch_async(queue2, ^{
NSLog(@"test1--%@",[NSThread currentThread]);
});
dispatch_async(queue2, ^{
NSLog(@"test2--%@",[NSThread currentThread]);
});
dispatch_async(queue2, ^{
NSLog(@"test3--%@",[NSThread currentThread]);
});
dispatch_async(queue2, ^{
NSLog(@"test4--%@",[NSThread currentThread]);
});
//全局队列的同步操作
//不开启新的线程,顺序执行
dispatch_sync(queue2, ^{
NSLog(@"--test1--%@",[NSThread currentThread]);
});
dispatch_sync(queue2, ^{
NSLog(@"--test2--%@",[NSThread currentThread]);
});
dispatch_sync(queue2, ^{
NSLog(@"--test3--%@",[NSThread currentThread]);
});
//串行队列的异步操作
//创建一个新的线程,顺序执行
dispatch_async(queue3, ^{
NSLog(@"--test1--%@",[NSThread currentThread]);
});
dispatch_async(queue3, ^{
NSLog(@"--test2--%@",[NSThread currentThread]);
});
dispatch_async(queue3, ^{
NSLog(@"--test3--%@",[NSThread currentThread]);
});
dispatch_async(queue3, ^{
NSLog(@"--test4--%@",[NSThread currentThread]);
});
//串行队列的同步操作
//不创建新的线程,顺序执行
dispatch_sync(queue3, ^{
NSLog(@"----1----%@",[NSThread currentThread]);
});
dispatch_sync(queue3, ^{
NSLog(@"----2----%@",[NSThread currentThread]);
});
dispatch_sync(queue3, ^{
NSLog(@"----3----%@",[NSThread currentThread]);
});
//并行队列的异步操作
//创建多个线程,异步操作,随机执行
dispatch_async(quqeue4, ^{
NSLog(@"----1----%@",[NSThread currentThread]);
});
dispatch_async(quqeue4, ^{
NSLog(@"----2----%@",[NSThread currentThread]);
});
dispatch_async(quqeue4, ^{
NSLog(@"----3----%@",[NSThread currentThread]);
});
dispatch_async(quqeue4, ^{
NSLog(@"----4----%@",[NSThread currentThread]);
});
// 并行队列的同步操作
// 不创建新的线程,顺序执行
dispatch_sync(quqeue4, ^{
NSLog(@"----1----%@",[NSThread currentThread]);
});
dispatch_sync(quqeue4, ^{
NSLog(@"----2----%@",[NSThread currentThread]);
});
dispatch_sync(quqeue4, ^{
NSLog(@"----3----%@",[NSThread currentThread]);
});
dispatch_sync(quqeue4, ^{
NSLog(@"----4----%@",[NSThread currentThread]);
});
}
-(void)test2{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@"--1--");
});
}
-(void)test3{
//计时器
[NSTimer scheduledTimerWithTimeInterval:4.0 target:self selector:@selector(task) userInfo:nil repeats:NO];
//阻塞线程,不推荐使用
[NSThread sleepForTimeInterval:2.0];
[self task];
//新建一个线程,两秒后执行,在全局队列中执行,不影响主线程执行
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_global_queue(0, 0), ^{
[self task];
});
NSLog(@"=====");
}
-(void)test4{
//<#size_t iterations#> 要重复几次 <#dispatch_queue_t queue#> 在哪个队列中 <#^(size_t)block#> 带有参数的Block, index的作用是为了按执行的顺序区分各个Block
dispatch_apply(10, dispatch_get_global_queue(0, 0), ^(size_t index) {
NSLog(@"第%ld个,thread:%@",index,[NSThread currentThread]);
});
//nslog一定在最后输出,dispatch_apply 会等待所有的执行结束后才继续执行
NSLog(@"--------");
}
-(void)test5{
dispatch_queue_t queue= dispatch_get_global_queue(0, 0);
dispatch_group_t group=dispatch_group_create();
dispatch_group_async(group, queue, ^{
NSLog(@"----1----%@",[NSThread currentThread]);
});
dispatch_group_async(group, queue, ^{
NSLog(@"----2----%@",[NSThread currentThread]);
});
dispatch_group_async(group, queue, ^{
NSLog(@"----3----%@",[NSThread currentThread]);
});
//监听 group执行完输出
dispatch_group_notify(group, queue, ^{
NSLog(@"end");
});
}
-(void)test6{
dispatch_queue_t queue=dispatch_queue_create("hhjjj", 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_sync(queue, ^{
NSLog(@"-----%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"----4----%@",[NSThread currentThread]);
});
}
-(void)task{
NSLog(@"run%@",[NSThread currentThread]);
}
@end