天天看点

iOS 09-GCD多线程基础

一、任务与队列:

1、执行任务的方式:

用同步的方式执行任务

dispatch_sync(dispatch_queue_t queue, dispatch_block_t block);

queue: 队列

block: 任务

用异步的方式执行任务

dispatch_async(dispatch_queue_t queue, dispatch_block_t block);

同步和异步的区别:

同步:只能在当前线程中执行任务,不具备开启新线程的能力

异步:可以在新的线程中执行任务,具备开启新线程的能力

二、队列的创建

并发和串行的区别:

并发:多个任务并发执行

串行:一个任务完成后,再执行下一个任务

串行队列的创建:

GCD提供两种获得串行队列的方式:

1、使用dispatch_queue_create函数创建串行队列

dispatch_queue_create(const char  *label, dispatch_queue_attr_t attr)

label: 表示队列的标识

attr:表示队列的属性,根据这个参数创建并行或者串行(串行使用

DISPATCH_QUEUE_SERIAL或者 NULL参数)

2、使用主队列(与主线程相关联的队列)

主队列是GCD自带的一种特殊的串行队列

放到主队列的任务都会在主线程中执行

使用dispatch_get_main_queue()获得主队列

并行队列的创建:

1、使用dispatch_queue_create函数创建串行队列

dispatch_queue_create(const char  *label, dispatch_queue_attr_t attr)

label: 表示队列的标识

attr:表示队列的属性,根据这个参数创建并行队列还是串行队列(并行使用

DISPATCH_QUEUE_CONCURRENT参数)

2、使用dispatch_get_global_queue函数获得全局的并发(GCD提供)

dispatch_get_global_queue(<#long identifier#>, <#unsigned long flags#>)

 第一个参数:队列的优先级

DISPATCH_QUEUE_PRIORITY_HIGH 2 //高

DISPATCH_QUEUE_PRIORITY_DEFAULT 0 //默认

DISPATCH_QUEUE_PRIORITY_LOW (-2) //低

DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN //后台

例:

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

第二个参数:占时无用,传入0即可

代码展示使用方法:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

}

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

//    [self concurrentSync];

//    [self concurrentAsync];

//    [self globalSync];

//    [self globalAsync];

//    [self serialSync];

//    [self serialAsync];

//    [self mainSync];

    [selfmainASync];

}

#pragma mark ---并行队列

//并发队列 +同步任务:没有开启新的线程,任务是逐个执行的

-(void)concurrentSync {

    //创建并发队列

    dispatch_queue_t queue =dispatch_queue_create("myQueue",DISPATCH_QUEUE_CONCURRENT);

    //在队列里面添加任务

    //同步任务

    dispatch_sync(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"----1----%@", [NSThreadcurrentThread]);

        }

    });

    dispatch_sync(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"----2----%@", [NSThreadcurrentThread]);

        }

    });

    dispatch_sync(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"----3----%@", [NSThreadcurrentThread]);

        }

    });

}

//并发队列 +异步任务:开启了新线程,任务是并发的。

-(void)concurrentAsync {

    //创建并发任务

    dispatch_queue_t queue =dispatch_queue_create("queue",DISPATCH_QUEUE_CONCURRENT);

    //创建异步任务

    dispatch_async(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"---1---%@", [NSThreadcurrentThread]);

        }

    });

    dispatch_async(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"---2---%@", [NSThreadcurrentThread]);

        }

    });

    dispatch_async(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"---3---%@", [NSThreadcurrentThread]);

        }

    });

}

//全局队列 +同步任务 :没有开启新线程,任务逐个完成的

-(void)globalSync {

    //获得全局队列

    //第一个参数:队列优先级这里使用默认,第二个参数:一般传入0

    dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

    //创建同步任务:

    dispatch_sync(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"----1----%@", [NSThreadcurrentThread]);

        }

    });

    dispatch_sync(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"----2----%@", [NSThreadcurrentThread]);

        }

    });

    dispatch_sync(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"----3----%@", [NSThreadcurrentThread]);

        }

    });

}

//全局队列 +异步任务: 开启了新的线程任务并发完成

-(void)globalAsync {

    //获得全局队列

    //第一个参数:队列优先级这里使用默认,第二个参数:一般传入0

    dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

    //创建异步任务

    dispatch_async(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"---1---%@", [NSThreadcurrentThread]);

        }

    });

    dispatch_async(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"---2---%@", [NSThreadcurrentThread]);

        }

    });

    dispatch_async(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"---3---%@", [NSThreadcurrentThread]);

        }

    });

}

#pragma mark ---串行队列

//串行队列 +同步任务 没有开启新线程,任务逐个完成

-(void)serialSync {

    //创建串行队列第一个参数:队列名称  第二个参数:创建的队列类型(DISPATCH_QUEUE_SERIAL或者NULL)为串行队列

    dispatch_queue_t queue =dispatch_queue_create("queue",DISPATCH_QUEUE_SERIAL);

    //创建同步任务

    dispatch_sync(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"---1---%@", [NSThreadcurrentThread]);

        }

    });

    dispatch_sync(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"---2---%@", [NSThreadcurrentThread]);

        }

    });

    dispatch_sync(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"---3---%@", [NSThreadcurrentThread]);

        }

    });

}

// 串行队列 +异步任务: 开启新的线程,任务是逐个完成的

-(void)serialAsync {

    //创建串行队列第一个参数:队列名称  第二个参数:创建的队列类型(DISPATCH_QUEUE_SERIAL或者NULL)为串行队列

    dispatch_queue_t queue =dispatch_queue_create("queue",DISPATCH_QUEUE_SERIAL);

    //创建异步任务

    dispatch_async(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"---1---%@", [NSThreadcurrentThread]);

        }

    });

    dispatch_async(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"---2---%@", [NSThreadcurrentThread]);

        }

    });

    dispatch_async(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"---3---%@", [NSThreadcurrentThread]);

        }

    });

}

//主队列 +同步任务:会造成死锁,不能在主队列中年添加同步任务

-(void)mainSync {

    //获得主队列

    dispatch_queue_t queue =dispatch_get_main_queue();

    //创建同步任务

    dispatch_sync(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"---1---%@", [NSThreadcurrentThread]);

        }

    });

    dispatch_sync(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"---2---%@", [NSThreadcurrentThread]);

        }

    });

    dispatch_sync(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"---3---%@", [NSThreadcurrentThread]);

        }

    });

}

//主队列 +异步任务:没有开启新线程 任务还是逐个完成的

-(void)mainASync {

    //获得主队列

    dispatch_queue_t queue =dispatch_get_main_queue();

    //创建同步任务

    dispatch_async(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"---1---%@", [NSThreadcurrentThread]);

        }

    });

    dispatch_async(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"---2---%@", [NSThreadcurrentThread]);

        }

    });

    dispatch_async(queue, ^{

        for (int i =0; i < 5; i++) {

            NSLog(@"---3---%@", [NSThreadcurrentThread]);

        }

    });

}

- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

上一篇: GCD栅栏
下一篇: gcd,lcm

继续阅读