GCD....使用
来源:互联网 发布:资海网络集团电话 编辑:程序博客网 时间:2024/06/08 09:20
//// ViewController.m// GCDDemo//// Created by 赵伟争 on 2016/11/9.// Copyright © 2016年 zwz. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; //异步执行 + 并行队列 [self asyncConcurrent]; //异步执行 + 串行队列 [self asyncSerial]; //同步执行 + 并行队列 [self syncConcurrent]; //同步执行+ 串行队列 [self syncSerial];}//异步执行 + 并行队列- (void)asyncConcurrent { //创建一个并行队列 dispatch_queue_t queue = dispatch_queue_create("com.dispatch.concurrent", DISPATCH_QUEUE_CONCURRENT);//生成一个并发执行队列,block被分发到多个线程去执行 concurrent 并发 NSLog(@"------start------"); //使用异步函数封装三个任务 dispatch_async(queue, ^{ NSLog(@"任务1--- %@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"任务2--- %@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"任务3--- %@", [NSThread currentThread]); }); NSLog(@"----end-----");}//异步执行 + 串行队列- (void)asyncSerial { //创建一个串行队列 dispatch_queue_t queue = dispatch_queue_create("com.dispatch.serial", DISPATCH_QUEUE_SERIAL); // serial 串行 NSLog(@"---start---"); //使用异步函数封装三个任务 dispatch_async(queue, ^{ NSLog(@"任务1---%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"任务2---%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"任务3---%@", [NSThread currentThread]); }); NSLog(@"---end---");}//同步执行 + 并行队列- (void)syncConcurrent { dispatch_queue_t queue = dispatch_queue_create("com.nodispatch.concurrent", DISPATCH_QUEUE_CONCURRENT); NSLog(@"---start---"); //使用同步函数封装三个任务 dispatch_sync(queue, ^{ NSLog(@"任务1---%@", [NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"任务2---%@", [NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"任务3---%@", [NSThread currentThread]); }); NSLog(@"---end---");}//同步执行+ 串行队列- (void)syncSerial{ dispatch_queue_t queue = dispatch_queue_create("com.nodispatch.serial", DISPATCH_QUEUE_SERIAL); NSLog(@"---start---"); //使用异步函数封装三个任务 dispatch_sync(queue, ^{ NSLog(@"任务1---%@", [NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"任务2---%@", [NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"任务3---%@", [NSThread currentThread]); }); NSLog(@"---end---");}//异步执行+主队列- (void)asyncMain{ //获取主队列 dispatch_queue_t queue = dispatch_get_main_queue(); NSLog(@"---start---"); //使用异步函数封装三个任务 dispatch_async(queue, ^{ NSLog(@"任务1---%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"任务2---%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"任务3---%@", [NSThread currentThread]); }); NSLog(@"---end---");}//同步执行+主队列(死锁)- (void)syncMain{ //获取主队列 dispatch_queue_t queue = dispatch_get_main_queue(); NSLog(@"---start---"); //使用同步函数封装三个任务 dispatch_sync(queue, ^{ NSLog(@"任务1---%@", [NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"任务2---%@", [NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"任务3---%@", [NSThread currentThread]); }); NSLog(@"---end---");}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
0 0
- 使用GCD
- 使用GCD
- 使用GCD
- 使用GCD
- 使用GCD
- 使用GCD
- 使用GCD
- 使用GCD
- 使用GCD
- 使用GCD
- 使用GCD
- 使用GCD
- GCD使用
- 使用GCD
- gcd 使用
- 使用GCD
- 使用GCD
- 使用GCD
- Android中,xml代码注释注意事项
- Android NDK JNI 简单调试 demo
- 分区(partitioning)概述
- Allegro出图1-钻孔文件
- UVA 11729
- GCD....使用
- android 打造真正的下拉刷新上拉加载recyclerview(四):自动加载和其他封装
- eclipse安装或者更新svn插件
- Posix匿名信号量与fork
- 网络编程常用字符串转换函数
- Get IT技能百科库 50个领域轻松直达
- 洛谷 P1133 教主的花园
- python_scapy构造ping程序
- c++设计模式之组合模式(composite)