226,GCD实例演示

来源:互联网 发布:大数据对日常生活影响 编辑:程序博客网 时间:2024/04/30 08:02

#import "ViewController.h"


@interface ViewController ()


@property (nonatomic,strong)dispatch_queue_t concurrentQueue;

@property (nonatomic,strong)dispatch_queue_t serialQueue;

@property (nonatomic,strong)dispatch_queue_t mainQueue;



@end


@implementation ViewController



- (dispatch_queue_t)concurrentQueue{

    if (_concurrentQueue ==nil) {

        _concurrentQueue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

    }

    return_concurrentQueue;

}


- (dispatch_queue_t)serialQueue{

    if (_serialQueue ==nil) {

        _serialQueue =dispatch_queue_create("cn.ljs",NULL);;

    }

    return_serialQueue;

}


- (dispatch_queue_t)mainQueue{

    if (_mainQueue ==nil) {

        _mainQueue =dispatch_get_main_queue();

    }

    return_mainQueue;

}


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

    //[self asynchronousConcurrent];

    //[self asynchronouseSerial];

    //[self asynchronouseMain];

    //[self synchronousConcurrent];

    [selfsynchronouseSerial];

}


//异步,并发队列:开了多条子线程,同时执行多个任务

- (void)asynchronousConcurrent{

    dispatch_async(self.concurrentQueue, ^{

       NSLog(@"-----%@-------下载图片1--------",[NSThreadcurrentThread]);

    });

    dispatch_async(self.concurrentQueue, ^{

        NSLog(@"-----%@-------下载图片2--------",[NSThreadcurrentThread]);

    });

    dispatch_async(self.concurrentQueue, ^{

        NSLog(@"-----%@-------下载图片3--------",[NSThreadcurrentThread]);

    });

    dispatch_async(self.concurrentQueue, ^{

        NSLog(@"-----%@-------下载图片4--------",[NSThreadcurrentThread]);

    });

    dispatch_async(self.concurrentQueue, ^{

        NSLog(@"-----%@-------下载图片5--------",[NSThreadcurrentThread]);

    });

}


//2016-01-21 13:03:57.206 5GCD实例[1056:110598] -----<NSThread: 0x7fabd0e032a0>{number = 5, name = (null)}-------下载图片4--------

//2016-01-21 13:03:57.206 5GCD实例[1056:110599] -----<NSThread: 0x7fabd0d1f290>{number = 6, name = (null)}-------下载图片5--------

//2016-01-21 13:03:57.206 5GCD实例[1056:110385] -----<NSThread: 0x7fabd2904550>{number = 4, name = (null)}-------下载图片3--------

//2016-01-21 13:03:57.206 5GCD实例[1056:110384] -----<NSThread: 0x7fabd2b0e2c0>{number = 3, name = (null)}-------下载图片2--------

//2016-01-21 13:03:57.206 5GCD实例[1056:110383] -----<NSThread: 0x7fabd0e03890>{number = 2, name = (null)}-------下载图片1--------


//异步,串行队列:会创建一条子线程,按照队列顺序,任务一个接着一个地执行

- (void)asynchronouseSerial{

    

    dispatch_async(self.serialQueue, ^{

        NSLog(@"-----%@-------下载图片1--------",[NSThreadcurrentThread]);

    });

    dispatch_async(self.serialQueue, ^{

        NSLog(@"-----%@-------下载图片2--------",[NSThreadcurrentThread]);

    });

    dispatch_async(self.serialQueue, ^{

        NSLog(@"-----%@-------下载图片3--------",[NSThreadcurrentThread]);

    });

    dispatch_async(self.serialQueue, ^{

        NSLog(@"-----%@-------下载图片4--------",[NSThreadcurrentThread]);

    });

    dispatch_async(self.serialQueue, ^{

        NSLog(@"-----%@-------下载图片5--------",[NSThreadcurrentThread]);

    });

}


//2016-01-21 13:20:54.017 5GCD实例[1295:122912] -----<NSThread: 0x7fdc9051ca20>{number = 2, name = (null)}-------下载图片1--------

//2016-01-21 13:20:54.018 5GCD实例[1295:122912] -----<NSThread: 0x7fdc9051ca20>{number = 2, name = (null)}-------下载图片2--------

//2016-01-21 13:20:54.018 5GCD实例[1295:122912] -----<NSThread: 0x7fdc9051ca20>{number = 2, name = (null)}-------下载图片3--------

//2016-01-21 13:20:54.018 5GCD实例[1295:122912] -----<NSThread: 0x7fdc9051ca20>{number = 2, name = (null)}-------下载图片4--------

//2016-01-21 13:20:54.018 5GCD实例[1295:122912] -----<NSThread: 0x7fdc9051ca20>{number = 2, name = (null)}-------下载图片5--------


//异步,主队列(主队列就是一个串行队列):不会创建子线程,只在主线程中按照串行队列顺序一个接着一个地执行

- (void)asynchronouseMain{

    

    dispatch_async(self.mainQueue, ^{

        NSLog(@"-----%@-------下载图片1--------",[NSThreadcurrentThread]);

    });

    dispatch_async(self.mainQueue, ^{

        NSLog(@"-----%@-------下载图片2--------",[NSThreadcurrentThread]);

    });

    dispatch_async(self.mainQueue, ^{

        NSLog(@"-----%@-------下载图片3--------",[NSThreadcurrentThread]);

    });

    dispatch_async(self.mainQueue, ^{

        NSLog(@"-----%@-------下载图片4--------",[NSThreadcurrentThread]);

    });

    dispatch_async(self.mainQueue, ^{

        NSLog(@"-----%@-------下载图片5--------",[NSThreadcurrentThread]);

    });

}


//2016-01-21 13:29:49.524 5GCD实例[1383:127064] -----<NSThread: 0x7fc2e0f034a0>{number = 1, name = main}-------下载图片1--------

//2016-01-21 13:29:49.525 5GCD实例[1383:127064] -----<NSThread: 0x7fc2e0f034a0>{number = 1, name = main}-------下载图片2--------

//2016-01-21 13:29:49.525 5GCD实例[1383:127064] -----<NSThread: 0x7fc2e0f034a0>{number = 1, name = main}-------下载图片3--------

//2016-01-21 13:29:49.526 5GCD实例[1383:127064] -----<NSThread: 0x7fc2e0f034a0>{number = 1, name = main}-------下载图片4--------

//2016-01-21 13:29:49.526 5GCD实例[1383:127064] -----<NSThread: 0x7fc2e0f034a0>{number = 1, name = main}-------下载图片5--------



//同步,并发队列:不会创建子线程,只在主线程中按照串行队列顺序一个接着一个地执行

- (void)synchronousConcurrent{

    dispatch_sync(self.concurrentQueue, ^{

        NSLog(@"-----%@-------下载图片1--------",[NSThreadcurrentThread]);

    });

    dispatch_sync(self.concurrentQueue, ^{

        NSLog(@"-----%@-------下载图片2--------",[NSThreadcurrentThread]);

    });

    dispatch_sync(self.concurrentQueue, ^{

        NSLog(@"-----%@-------下载图片3--------",[NSThreadcurrentThread]);

    });

    dispatch_sync(self.concurrentQueue, ^{

        NSLog(@"-----%@-------下载图片4--------",[NSThreadcurrentThread]);

    });

    dispatch_sync(self.concurrentQueue, ^{

        NSLog(@"-----%@-------下载图片5--------",[NSThreadcurrentThread]);

    });

}


//2016-01-21 13:36:41.269 5GCD实例[1439:130167] -----<NSThread: 0x7fe2d4104ce0>{number = 1, name = main}-------下载图片1--------

//2016-01-21 13:36:41.270 5GCD实例[1439:130167] -----<NSThread: 0x7fe2d4104ce0>{number = 1, name = main}-------下载图片2--------

//2016-01-21 13:36:41.270 5GCD实例[1439:130167] -----<NSThread: 0x7fe2d4104ce0>{number = 1, name = main}-------下载图片3--------

//2016-01-21 13:36:41.270 5GCD实例[1439:130167] -----<NSThread: 0x7fe2d4104ce0>{number = 1, name = main}-------下载图片4--------

//2016-01-21 13:36:41.270 5GCD实例[1439:130167] -----<NSThread: 0x7fe2d4104ce0>{number = 1, name = main}-------下载图片5--------



//同步,串行队列:会创建一条子线程,按照队列顺序,任务一个接着一个地执行

- (void)synchronouseSerial{

    dispatch_sync(self.serialQueue, ^{

        NSLog(@"-----%@-------下载图片1--------",[NSThreadcurrentThread]);

    });

    dispatch_sync(self.serialQueue, ^{

        NSLog(@"-----%@-------下载图片2--------",[NSThreadcurrentThread]);

    });

    dispatch_sync(self.serialQueue, ^{

        NSLog(@"-----%@-------下载图片3--------",[NSThreadcurrentThread]);

    });

    dispatch_sync(self.serialQueue, ^{

        NSLog(@"-----%@-------下载图片4--------",[NSThreadcurrentThread]);

    });

    dispatch_sync(self.serialQueue, ^{

        NSLog(@"-----%@-------下载图片5--------",[NSThreadcurrentThread]);

    });

}


//2016-01-21 13:39:16.764 5GCD实例[1502:133241] -----<NSThread: 0x7fa2a42037f0>{number = 1, name = main}-------下载图片1--------

//2016-01-21 13:39:16.764 5GCD实例[1502:133241] -----<NSThread: 0x7fa2a42037f0>{number = 1, name = main}-------下载图片2--------

//2016-01-21 13:39:16.765 5GCD实例[1502:133241] -----<NSThread: 0x7fa2a42037f0>{number = 1, name = main}-------下载图片3--------

//2016-01-21 13:39:16.765 5GCD实例[1502:133241] -----<NSThread: 0x7fa2a42037f0>{number = 1, name = main}-------下载图片4--------

//2016-01-21 13:39:16.765 5GCD实例[1502:133241] -----<NSThread: 0x7fa2a42037f0>{number = 1, name = main}-------下载图片5--------


//同步,主队列:会死锁,不能用



//总结:前三种方法是常用,需要掌握


@end


0 0
原创粉丝点击