GCD多线程
来源:互联网 发布:8寸windows平板电脑 编辑:程序博客网 时间:2024/05/20 09:10
//串行队列,线性同步
- (IBAction)handleSyncQueue:(UIButton*)sender {
//1.获取串行队列
//(1)获取系统创建好的串行队列,主队列,在主线程中执行
//dispatch_queue_t queue = dispatch_get_main_queue();
//(2)自己创建串行队列,在子线程中执行
dispatch_queue_t queue =dispatch_queue_create("com.lanou3g.wang",DISPATCH_QUEUE_SERIAL);
//2.往队列中添加任务
dispatch_async(queue, ^{
NSLog(@"任务一,当前线程%@", [NSThreadcurrentThread]);
});
dispatch_async(queue, ^{
NSLog(@"任务二,当前线程%@", [NSThreadcurrentThread]);
});
dispatch_async(queue, ^{
NSLog(@"任务三,当前线程%@", [NSThreadcurrentThread]);
});
dispatch_async(queue, ^{
NSLog(@"任务四,当前线程%@", [NSThreadcurrentThread]);
});
}
//并行队列,线程并发
- (IBAction)handleConcurrentQueue:(UIButton*)sender {
//1.获取并行队列
//(1)获取系统创建好的并行队列
dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
//2.往队列中添加任务
dispatch_async(queue, ^{
NSLog(@"任务一,当前进程%@", [NSThreadcurrentThread]);
});
dispatch_async(queue, ^{
NSLog(@"任务二,当前进程%@", [NSThreadcurrentThread]);
});
dispatch_async(queue, ^{
NSLog(@"任务三,当前进程%@", [NSThreadcurrentThread]);
});
dispatch_async(queue, ^{
NSLog(@"任务四,当前进程%@", [NSThreadcurrentThread]);
});
}
//分组任务
- (IBAction)handleGroup:(UIButton*)sender {
//1.获取并发队列
dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
//2.创建分组
dispatch_group_t group =dispatch_group_create();
//往队列中添加分组
dispatch_group_async(group, queue, ^{
NSLog(@"请求0~1M数据,当前线程%@", [NSThreadcurrentThread]);
});
dispatch_group_async(group, queue, ^{
NSLog(@"请求1~2M数据,当前线程%@", [NSThreadcurrentThread]);
});
dispatch_group_async(group, queue, ^{
NSLog(@"请求2~3M数据,当前线程%@", [NSThreadcurrentThread]);
});
dispatch_group_async(group, queue, ^{
NSLog(@"请求3~4M数据,当前线程%@", [NSThreadcurrentThread]);
});
}
//整个程序运行期间,只执行一次
- (IBAction)handleOnce:(UIButton*)sender {
staticdispatch_once_tonceToken;
dispatch_once(&onceToken, ^{
//存放只会执行一次的代码,比如单例对象的创建.
});
}
//障碍
- (IBAction)handleBarrier:(UIButton*)sender {
//1.获取并发队列
//dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//如果要加障碍,必须要使用自己创建的队列
//自己创建并行队列
dispatch_queue_t queue =dispatch_queue_create("com.lanou3g.zhang",DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
NSLog(@"A,写入");
});
dispatch_async(queue, ^{
NSLog(@"B,写入");
});
dispatch_async(queue, ^{
NSLog(@"C,写入");
});
dispatch_barrier_async(queue, ^{
NSLog(@"障碍,读取数据");
});
dispatch_async(queue, ^{
NSLog(@"D,写入");
});
dispatch_async(queue, ^{
NSLog(@"E,写入");
});
dispatch_async(queue, ^{
NSLog(@"F,写入");
});
}
//延迟
- (IBAction)handleDelay:(UIButton*)sender {
double second = 10.0;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(second *NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
//存放延迟10.0之后的任务
NSLog(@"存放延迟10.0之后的任务");
});
}
//重复执行的任务
- (IBAction)handleApply:(id)sender {
dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
NSArray *arr = @[@"aa",@"bb",@"cc",@"dd",@"ee",@"ff",@"gg"];
//如果每次只执行一个任务,则在主线程中执行,如果并发执行多次,主线程执行之外,还会分配子线程
dispatch_apply([arrcount], queue, ^(size_tindex) {
NSLog(@"任务%lu,当前线程%@", index +1, [NSThreadcurrentThread]);
NSLog(@"%@", arr[index]);
});
}
阅读全文
0 0
- GCD多线程
- 多线程GCD
- GCD多线程
- 多线程-GCD
- 多线程 gcd
- 多线程GCD
- 多线程&GCD
- GCD多线程
- 多线程GCD
- 多线程:GCD
- 多线程-GCD
- 多线程GCD
- 多线程-GCD
- GCD(多线程)
- 多线程GCD
- 多线程GCD
- 多线程 GCD
- 多线程GCD
- spring之BeanFactory
- POJ 2718 Smallest Difference
- 1006.单词变换
- php隐藏手机号中间四位
- Java回调学习
- GCD多线程
- 比特位操作——O(1)时间检测2的幂次
- 06_ARM汇编自学笔记指令系统之分类与格式
- Buildroot构建指南--Overview
- Android TextInputLayout 使用
- 在pom.xml中设置maven的镜像为国内镜像
- linux恢复 rm -rf 删除的文件:extundelete
- hbase shell操作之scan+filter
- SharePoint 2016与外部系统人员信息同步(一,Excel数据导入到AD)