GCD-ios 多线程

#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值