GCD

作者: 轻轻语焉 | 来源:发表于2018-05-31 15:17 被阅读11次

    http://blog.csdn.net/wang631106979/article/details/51788622 

    https://developer.apple.com/documentation/dispatch#//apple_ref/c/func/dispatch_async

    - (void)testTargetQueue {

        //1.创建目标队列

        dispatch_queue_t targetQueue = dispatch_queue_create("test.target.queue", DISPATCH_QUEUE_SERIAL);

        //2.创建3个串行队列

        dispatch_queue_t queue1 = dispatch_queue_create("test.1", DISPATCH_QUEUE_SERIAL);

        dispatch_queue_t queue2 = dispatch_queue_create("test.2", DISPATCH_QUEUE_SERIAL);

        dispatch_queue_t queue3 = dispatch_queue_create("test.3", DISPATCH_QUEUE_SERIAL);

        //3.将3个串行队列分别添加到目标队列

        dispatch_set_target_queue(queue1, targetQueue);

        dispatch_set_target_queue(queue2, targetQueue);

        dispatch_set_target_queue(queue3, targetQueue);

        dispatch_async(queue1, ^{

            NSLog(@"1 in");

            [NSThread sleepForTimeInterval:3.f];

            NSLog(@"1 out");

        });

        dispatch_async(queue2, ^{

            NSLog(@"2 in");

            [NSThread sleepForTimeInterval:2.f];

            NSLog(@"2 out");

        });

        dispatch_async(queue3, ^{

            NSLog(@"3 in");

            [NSThread sleepForTimeInterval:1.f];

            NSLog(@"3 out");

        });

    }

    - (void)run{

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

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

        }

    }

    - (void)dispatchTest1{

        dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

        dispatch_group_t group = dispatch_group_create();

        dispatch_group_async(group, globalQueue, ^{

            int a = 10;

            while (a != 0) {

                a --;

            }

            NSLog(@"111111 %@",[NSThread currentThread]);

        });

        dispatch_group_async(group, globalQueue, ^{

            NSLog(@"2222222 %@",[NSThread currentThread]);

        });

        dispatch_group_async(group, globalQueue, ^{

            NSLog(@"3333333 %@",[NSThread currentThread]);

        });

    //    dispatch_group_notify(group, globalQueue, ^{

    //        NSLog(@"group Exeture End %@",[NSThread currentThread]);

    //    });

        dispatch_group_wait(group, DISPATCH_TIME_FOREVER);

        NSLog(@"------------%@",[NSThread currentThread]);

    }

    - (void)dispatchTest2{

    //    我们知道多线程开发最难的就是执行顺序的控制,苹果已经给我们封装好了一些流控制的东西,像dispatch_group,等但是有时候某些场景还是需要我们自己实现对代码执行的控制,毕竟我们不希望自己写的代码自己都不知道执行顺序,dispatch_semaphore就是为了这个目的而存在的,我们可以设置一个cout来控制程序按照我们的意愿来执行。

    //    注意:对于这样的阻塞线程的操作,最好不要放在主线程,除非特殊要求。我觉得这应该是我们用多线程开发的共识了。

        dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

        dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

        dispatch_queue_t customSerialQueue = dispatch_queue_create("kongqingyu", DISPATCH_QUEUE_SERIAL);

        dispatch_async(globalQueue, ^{

            dispatch_sync(customSerialQueue, ^{

                [NSThread sleepForTimeInterval:0.1];

                NSLog(@"-----------信号即将发送--------");

                dispatch_semaphore_signal(semaphore);

            });

        });

        NSLog(@"---------------wait----------");

        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

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

        dispatch_async(globalQueue, ^{

            dispatch_sync(customSerialQueue, ^{

                [NSThread sleepForTimeInterval:0.1];

                NSLog(@"-----------信号即将发送--------");

                dispatch_semaphore_signal(semaphore);

            });

        });

        NSLog(@"---------------wait----------");

        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

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

    //    dispatch_release(semaphore);

    }

    -(void)dispatchSignal{

        //crate的value表示,最多几个资源可访问

        dispatch_semaphore_t semaphore = dispatch_semaphore_create(2);

        dispatch_queue_t quene = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

        //任务1

        dispatch_async(quene, ^{

            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

            NSLog(@"run task 1");

            sleep(1);

            NSLog(@"complete task 1");

            dispatch_semaphore_signal(semaphore);

        });

        //任务2

        dispatch_async(quene, ^{

            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

            NSLog(@"run task 2");

            sleep(1);

            NSLog(@"complete task 2");

            dispatch_semaphore_signal(semaphore);

        });

        //任务3

        dispatch_async(quene, ^{

            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

            NSLog(@"run task 3");

            sleep(1);

            NSLog(@"complete task 3");

            dispatch_semaphore_signal(semaphore);

        });

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    /**

    1、dispatch_queue_t 队列

        (1) 队列的创建 dispatch_queue_create()

        dispatch_queue_create(const char *_Nullable label,

      dispatch_queue_attr_t _Nullable attr); //第一个参数:队列的名称

        第二个参数:若是Serial Dispatch Queue 可为NULL/DISPATCH_QUEUE_SERIAL。若为ConCurrent Dispatch Queue,则指定为 DISPATCH_QUEUE_CONCURRENT

        通过dispatch_queue_create() 创建的队列使用与默认优先级Global Dispatch Queue 相同执行优先级的线程。

        (2)获取系统标准提供的 Dispatch Queue

            串行(主线程):    dispatch_get_main_queue()

            并行: dispatch_get_global_queue() //有多种执行优先级

    2、dispatch_set_target_queue

        修改队列的优先级,通过create创建的队列,优先级 与 Global Dispatch Queue的默认优先级 一样。可以通过 dispatch_set_target_queue 方法修改 create 创建队列的优先级。

    3、dispatch_after 在 (一段时间)后 将指定的 Block 追加到(指定的)队列中来。

    4、dispatch_group

        使用Concurrent Dispatch Queue时或者同时使用多个Dispatch Queue 时没在多个处理全部结束时后执行结束处理。

    */

    相关文章

      网友评论

          本文标题:GCD

          本文链接:https://www.haomeiwen.com/subject/kgwubxtx.html