GCD-同步,异步分别在串行队列,并发队列,主队列下的执行方式

GCD-同步,异步分别在串行队列,并发队列,主队列下的执行方式   




- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //获取串行的队列
//   dispatch_queue_t singal =  dispatch_queue_create("singal", DISPATCH_QUEUE_SERIAL);
//    //获取并发执行的队列
//    dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_CONCURRENT);
//    //获取主队列
//   dispatch_queue_t mainQueue =  dispatch_get_main_queue();
//    //获取全局的队列(并发的)
//   dispatch_queue_t gobalqueue =  dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    


}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{


    [self mainSynQueue];


}


/**
*  串行队列异步执行任务
    1.异步具有创建新的线程的能力, 会开辟新的线程去执行任务
    2.按照串行的方式去执行任务
*/
- (void)singalAsynQueue{
    
    dispatch_queue_t singal =   dispatch_queue_create("singal", NULL);
    
    [self showAsynWithDispatch_queue_t:singal];
    
    
}


/**
*  串行队列同步执行任务
    1.同步不具有创建新的线程的能力, 不会开辟新的线程去执行任务,会在当前的程序的主线程中去执行任务
    2.按照串行的方式去执行任务
*/
- (void)singalSynQueue{
    
    dispatch_queue_t singal =   dispatch_queue_create("singal", NULL);
    
    [self showSynWithDispatch_queue_t:singal];
}


/**
*  并发队列异步执行任务(常用)
   1.异步不具有创建新的线程的能力,会开辟新的线程去执行任务,不会在当前的程序的主线程中去执行任务
   2.按照并发的方式去执行任务
*/
- (void)queueAsynQueue{
dispatch_queue_t queue =  dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
   
    [self showAsynWithDispatch_queue_t:queue];
    
}
/**
*  并发队列同步执行任务
   1.同步不具有创建新的线程的能力, 不会开辟新的线程去执行任务,会在当前的程序的主线程中去执行任务
   2.按照同步的方式去执行任务
*/
- (void)queueSynQueue{
    
    dispatch_queue_t queue =  dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  
    
    [self showSynWithDispatch_queue_t:queue];
    
}


/**
*  主队列的同步(会造成程序的死锁)
*/
- (void)mainAsynQueue{
    
  dispatch_queue_t main =  dispatch_get_main_queue();
    
    
    [self showSynWithDispatch_queue_t:main];
    
}


/**
*  主队列的异步(在主线程中顺序执行)
    新添加到主队列中的任务会放到队列的最尾部,等到当前主线程中的任务结束之后然后再从队列的头部取出依次执行(FIFO)仙进先出
*/
- (void)mainSynQueue{
    
    dispatch_queue_t main =  dispatch_get_main_queue();
    
    
    [self showAsynWithDispatch_queue_t:main];
    


    
    
}
/**
*  异步的演示 */
- (void)showAsynWithDispatch_queue_t:(dispatch_queue_t)queue{




    NSLog(@"%@",[NSThread currentThread]);
    
    dispatch_async(queue, ^{
        
        NSLog(@"----1----%@",[NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        
        NSLog(@"----2----%@",[NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        
        NSLog(@"----3----%@",[NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        
        NSLog(@"----4----%@",[NSThread currentThread]);
    });


    
    NSLog(@"--------end------------");


}
/**
*
*
*   同步的演示
*/
- (void)showSynWithDispatch_queue_t:(dispatch_queue_t)queue{
    
    
    NSLog(@"%@",[NSThread currentThread]);
    
    dispatch_sync(queue, ^{
        
        NSLog(@"----1----%@",[NSThread currentThread]);
    });
    
    dispatch_sync(queue, ^{
        
        NSLog(@"----2----%@",[NSThread currentThread]);
    });
    
    dispatch_sync(queue, ^{
        
        NSLog(@"----3----%@",[NSThread currentThread]);
    });
    
    dispatch_sync(queue, ^{
        
        NSLog(@"----4----%@",[NSThread currentThread]);
    });
    
    
    NSLog(@"--------end------------");


    
    
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值