| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
end5
10年前发布

GCD的常用方法总结

 //    GCD常用方法        //————————————————————————————————————————————————————————————————————————————————        //串行队列        dispatch_queue_t queueSerial = dispatch_queue_create("jr", DISPATCH_QUEUE_SERIAL);          //并行队列        dispatch_queue_t queueConcu = dispatch_queue_create("jr2", DISPATCH_QUEUE_CONCURRENT);        //1、循环多次使用        //(1)添加串行队列,同步执行。可以当for循环使用(在主线程中)    //    dispatch_apply(3, queueSerial, ^(size_t t) {// 参数^(size_t),需要自己加一个^(size_t t),否则有问题    //        [NSThread sleepForTimeInterval:1];    //        NSLog(@"%@", [NSThread currentThread]);    //    });        //(2)添加并行队列,同步执行。可以当for循环使用(如果循环次数大于1,则开子线程)    //    dispatch_apply(3, queueConcu, ^(size_t t) {// 参数^(size_t),需要自己加一个^(size_t t),否则有问题    //        [NSThread sleepForTimeInterval:1];    //        NSLog(@"%@", [NSThread currentThread]);    //    });                //(3)参数size_t的作用:打印的是0.1.2.3.。。的序号    //    dispatch_apply(3, queueSerial, ^(size_t t) {    //        [NSThread sleepForTimeInterval:1];    //        NSLog(@"%li == %@", t, [NSThread currentThread]);    //    });                //(4)以上都是在主线程中进行的。如果想在子线程中执行,需要这样做    //    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{    //        dispatch_apply(3, queueConcu, ^(size_t t) {    //            [NSThread sleepForTimeInterval:1];    //            NSLog(@"%li == %@", t, [NSThread currentThread]);    //        });    //    });                        //2、分组(可以监听组内子线程是否全部都执行完成)    //    dispatch_group_t group = dispatch_group_create();    //        //    dispatch_group_async(group, queueConcu, ^{    //        [NSThread sleepForTimeInterval:1];    //        NSLog(@"%@", [NSThread currentThread]);    //    });    //    dispatch_group_async(group, queueConcu, ^{    //        [NSThread sleepForTimeInterval:1];    //        NSLog(@"%@", [NSThread currentThread]);    //    });    //    dispatch_group_async(group, queueConcu, ^{    //        [NSThread sleepForTimeInterval:1];    //        NSLog(@"%@", [NSThread currentThread]);    //    });    //        //    dispatch_group_notify(group, queueConcu, ^{    //        NSLog(@"----%@", [NSThread currentThread]);    //    });                        //3、延迟(写在这个块中的代码都是在主线程中执行的)                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{            [NSThread sleepForTimeInterval:2];            NSLog(@"----%@", [NSThread currentThread]);        });         NSLog(@"33333%@", [NSThread currentThread]);        timer = [NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:@selector(touchesBegan) userInfo:nil repeats:YES];                 //4.设置障碍(在同一队列中,只要添加了障碍,不管有没有创建多线程,他后边的任务都要跨国这个障碍,即等待这个障碍结束)    //    dispatch_async(queueConcu, ^{    //        NSLog(@"11111");    //    });    //        //    dispatch_barrier_async(queueConcu, ^{    //        [NSThread sleepForTimeInterval:5];    //    });    //        //    dispatch_async(queueConcu, ^{    //        NSLog(@"11111");    //    });    //    dispatch_async(queueConcu, ^{    //        NSLog(@"11111");    //    });                        //5、让代码只执行一次    //    static dispatch_once_t oneToken;    //    dispatch_once(&oneToken, ^{    //        NSLog(@"=======");    //    });                //这个方法。常用在单例中