您的位置:首页 > 移动开发 > IOS开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: