多线程:GCD 基本使用
2016-03-25 23:22
281 查看
// // ViewController.m // 05-GCD基本使用 // // Created by gzxzmac on 16/1/29. // Copyright © 2016年 gzxzmac. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self gcdDemo3]; } /** 跟NSThread 对比 1. GCD使用block ,代码都放在一起,比较直观。NSThread 需要使用selector 来指定线程入口 2. GCD 回到主线程使用 dispatch_get_main_queue .NSThread 回到主线程,只能通过NSObject 的分类方法来实现 */ // 同步 - (void)gcdDemo { // 同步 // 任务 void (^task)() = ^ { NSLog(@"%@",[NSThread currentThread]); }; dispatch_sync(dispatch_get_global_queue(0, 0), task); } //异步 - (void)gcdDemo1 { void (^task)() = ^ { NSLog(@"%@",[NSThread currentThread]); }; dispatch_async(dispatch_get_global_queue(0, 0), task); } // 精简 - (void)gcdDemo2 { dispatch_async(dispatch_get_global_queue(0, 0), ^{ NSLog(@"%@",[NSThread currentThread]); }); } // 线程间通讯 - (void)gcdDemo3 { // 下载 dispatch_async(dispatch_get_global_queue(0, 0), ^{ NSLog(@"正在下载%@",[NSThread currentThread]); // 回到主线程刷新UI dispatch_sync(dispatch_get_main_queue(), ^{ NSLog(@"刷新UI%@",[NSThread currentThread]); }); }); } @end
相关文章推荐
- java 递归
- 弦月下的SQL<2> 数据库管理
- CentOS-6.5下编译安装LNMP环境【nginx1.8.1、mysql5.5.43、php 5.6】
- key word
- 游标使用效率对比
- 多线程:block 演练
- Spring事务管理(详解+实例)
- 职场“潜”规则:心法和技法
- SQL语言简介
- zk-002 zookeeper的基本概念
- 多线程:UIView 动画案例
- Pycharm使用笔记
- 图片异步加载之 Android-Universal-Image-Loader
- 第七届蓝桥杯省赛8:四平方和
- 如何快速部署自己的插件到Nexus服务器(P2 Update site)?
- java io操作
- 多线程:同步与异步
- 2016第11周五
- Tomcat-Jdbc-Pool连接池参数说明
- js正则表达式用法汇总