GCD-ios 多线程
2015-09-13 09:31
369 查看
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // [self createMyQueue]; //全局队列 // [self getGlobalQueue]; //主线程 [self getMainQueue]; // Do any additional setup after loading the view, typically from a nib. } #pragma mark -抓取主队列- -(void)getMainQueue{ dispatch_async(dispatch_get_main_queue(), ^{ //主线程获取方式 NSLog(@"%@",[NSThread currentThread]); NSLog(@"isMain:%d",[NSThread isMainThread]); NSLog(@"isMulti:%d",[NSThread isMultiThreaded]); [self printFirst]; [self printForth]; [self printSecond]; [self printThird]; [self printFinish]; }); } #pragma mark -GCD的使用- /** * 抓取全局队列 * */ -(void)getGlobalQueue{ //获取全局线程 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0); dispatch_async(queue, ^{ NSLog(@"%@",[NSThread currentThread]); NSLog(@"isMulti:%d",[NSThread isMultiThreaded]); [self printFirst]; }); //直接用 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSLog(@"queue2%@",[NSThread currentThread]); NSLog(@"isMulti2:%d",[NSThread isMultiThreaded]); [self printSecond]; }); dispatch_async(dispatch_get_global_queue(0, 0), ^{ NSLog(@"queue3%@",[NSThread currentThread]); NSLog(@"isMulti3:%d",[NSThread isMultiThreaded]); //3、4 串行 [self printThird]; [self printForth]; [self printFinish]; }); } //1.自己创建 //2.系统创建 //3.获取主线程 //自己创建队列 -(void)createMyQueue{ //派遣队列 //参数1:给一个运行标记 参数2:属性:一般传NULL dispatch_queue_t myQueue= dispatch_queue_create("myQueue", NULL); //1.同步派遣 串行 dispatch_sync(myQueue, ^{ NSLog(@"%@",[NSThread currentThread]); NSLog(@"%d",[NSThread isMultiThreaded]); [self printFirst]; }); dispatch_sync(myQueue, ^{ [self printSecond]; }); dispatch_sync(myQueue, ^{ [self printThird]; }); dispatch_sync(myQueue, ^{ [self printThird]; }); //2.异步演示 // dispatch_async(myQueue, ^{ // //异步创建 新的线程 // NSLog(@"%@",[NSThread currentThread]); // NSLog(@"%d",[NSThread isMultiThreaded]); // [self printThird]; // [self printSecond]; // [self printFirst]; // }); //3.创建组 dispatch_group_t group = dispatch_group_create(); dispatch_group_async(group, myQueue, ^{ [self printFirst]; }); } #pragma mark -创建几个任务- //打印任务 -(void)printFirst { for (int i = 0 ; i < 10; i++) { NSLog(@"任务1 执行 %d步",i); [NSThread sleepForTimeInterval:1.0]; } } -(void)printSecond { for (int i = 0 ; i < 10; i++) { NSLog(@"任务2 执行 %d步",i); [NSThread sleepForTimeInterval:1.0]; } } -(void)printThird { for (int i = 0 ; i < 10; i++) { NSLog(@"任务3 执行 %d步",i); [NSThread sleepForTimeInterval:1.0]; } } -(void)printForth { for (int i = 0 ; i < 10; i++) { NSLog(@"任务4 执行 %d步",i); [NSThread sleepForTimeInterval:1.0]; } } -(void)printFinish { for (int i = 0 ; i < 10; i++) { NSLog(@"任务5 执行 %d步",i); [NSThread sleepForTimeInterval:1.0]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
相关文章推荐
- 【IOS 开发学习总结-OC-7.3】C 语言特性——动态存储与静态存储
- iOS学习总结----通过代码自定义cell(cell的⾼度不⼀致)
- iOS中两个视图的翻转
- 猫猫学iOS 之控制器view显示中view的父子关系及controller的父子关系_解决屏幕旋转不能传递事件问题
- 【IOS 开发学习总结-OC-7.2】C 语言特性——局部变量与全局变量
- ios 检测应用程序升级问题
- Mac环境下svn的使用
- iOS开发:一个瀑布流的设计与实现(已实现缓存池功能,该功能使得瀑布流cell可以循环利用)
- 相册的实现
- 滚动界面的实现
- Ios中的普通屏幕和retina屏幕的了解
- [ios 程序启动与运转] - RunLoop个人小结
- iOS开发脚踏实地学习day07-美团
- Multithreading and Grand Central Dispatch on iOS for Beginners Tutorial
- iOS 多语言本地化 完美解决方案【自动+手动】
- iOS开发 ----- 仿QQ实现滑动显示后边按钮
- IOS 调用系统键盘 设置搜索字段和事件
- XML parser for iOS
- iOS 应用程序内部国际化,不跟随系统语言
- 【IOS 开发学习总结-OC-7.1】C 语言特性——函数