美文网首页
iOS多线程之GCD

iOS多线程之GCD

作者: d50486b28313 | 来源:发表于2017-05-06 15:26 被阅读0次

    简单的使用:

        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            NSLog(@"异步进行操作");
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"回到主线程,一般可用来刷新ui等操作");
            });
        });
    

    给异步线程设置优先级:

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        });
    

    创建自定义串行线程:

        dispatch_queue_t queue = dispatch_queue_create("com.test.gcd.queue", NULL);
        
        dispatch_async(queue, ^{
            NSLog(@"start task 1");
            [NSThread sleepForTimeInterval:2];
            NSLog(@"stop task 1");
        });
        
        dispatch_async(queue, ^{
            NSLog(@"start task 2");
            [NSThread sleepForTimeInterval:2];
            NSLog(@"stop task 2");
        });
    

    创建自定义并行线程:

        dispatch_queue_t queue = dispatch_queue_create("com.test.gcd.queue", DISPATCH_QUEUE_CONCURRENT);
        
        dispatch_async(queue, ^{
            NSLog(@"start task 1");
            [NSThread sleepForTimeInterval:2];
            NSLog(@"stop task 1");
        });
        
        dispatch_async(queue, ^{
            NSLog(@"start task 2");
            [NSThread sleepForTimeInterval:2];
            NSLog(@"stop task 2");
        });
    

    如果需要当所有的异步线程结束之后调用同一个函数,可以使用GCD Group:

        dispatch_queue_t queue = dispatch_queue_create("com.test.gcd.group", DISPATCH_QUEUE_CONCURRENT);
        
        dispatch_group_t group = dispatch_group_create();
        
        dispatch_group_async(group, queue, ^{
            NSLog(@"start task 1");
            [NSThread sleepForTimeInterval:2];
            NSLog(@"stop task 1");
        });
        
        dispatch_group_async(group, queue, ^{
            NSLog(@"start task 2");
            [NSThread sleepForTimeInterval:2];
            NSLog(@"stop task 2");
        });
        
        dispatch_group_notify(group, queue, ^{
            NSLog(@"All task over");
        })
    

    将异步线程加入到GCD group当中(注意:enter和leave函数必须成对出现,且leave函数应该放在异步线程的结束位置):

        dispatch_group_enter(group);
        [self sendRequest1:^{
            NSLog(@"request1 done");
            dispatch_group_leave(group);
        }];
        
        dispatch_group_enter(group);
        [self sendRequest2:^{
            NSLog(@"request2 done");
            dispatch_group_leave(group);
        }];
    
        dispatch_group_notify(group, queue, ^{
            NSLog(@"All task over");
        });
    
        - (void)sendRequest1:(void(^)())block {
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                NSLog(@"start task 1");
                [NSThread sleepForTimeInterval:2];
                NSLog(@"stop task 1");
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (block) {
                        block();
                    }
                });
            });
        }
    
        - (void)sendRequest2:(void(^)())block {
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                NSLog(@"start task 2");
                [NSThread sleepForTimeInterval:2];
                NSLog(@"stop task 2”);
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (block) {
                        block();
                    }
                });
            });
        }
    

    单例(只会执行一次):

        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            NSLog(@"excute only once");
        });
    

    延迟操作(开始了就不能取消):

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"delay 2 sec");
        });
    

    相关文章

      网友评论

          本文标题:iOS多线程之GCD

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