GCD-ios 多线程
来源:互联网 发布:java找工作好难 编辑:程序博客网 时间:2024/05/22 15:55
#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
0 0
- IOS 多线程 GCD
- GCD ios多线程 runloop
- ios学习 -- GCD多线程
- iOS多线程GCD
- iOS GCD 多线程
- iOS多线程GCD
- iOS多线程GCD
- GCD ios多线程 runloop
- iOS 多线程之----------GCD
- IOS多线程之GCD
- iOS多线程GCD
- iOS 多线程3 GCD
- IOS多线程GCD
- GCD ios多线程 runloop
- IOS 多线程之GCD
- ios多线程-GCD
- iOS多线程GCD
- GCD ios多线程 runloop
- LeetCode----House Robber
- 收集的Android开源框架及其地址
- 还在用XListView?试试更漂亮的AutoLoadListView吧!
- 一名程序媛的软件开发之路
- 我的心理健康
- GCD-ios 多线程
- LeetCode_OJ【185】Department Top Three Salaries
- jdbc 封装
- 复杂度之双边滤波算法的原理、流程、实现及效果。
- GCD创建单例的方法
- 百度机试:突出重围
- 白盒测试用例设计方法
- 谷歌新Logo如何做到只有305字节
- 浙江大学PAT_乙级_1042. 字符统计(20)