GCD学习(一)

IOS使用多线程有三种方法NSThread、NSOperation和NSOperationQueue、GCD;

GCD学习(一)


(1)异步和同步

dispatch_sync(queue, ^{
            NSLog(@"这是同步线程");
        });
        
        dispatch_async(queue, ^{
            NSLog(@"这是异步线程");
        });


(2)并行队列和串行队列

//并行队列
    //dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);//系统默认的并行队列
    dispatch_queue_t concurrentQuueue = dispatch_queue_create("com.itt.crq", DISPATCH_QUEUE_CONCURRENT);//自定义并行队列
    dispatch_async(concurrentQuueue, ^{
        
        NSLog(@"task1");
    });
    dispatch_async(concurrentQuueue, ^{
        
        NSLog(@"task2");
    });
    
    
    //串行队列
    dispatch_queue_t serialQueue = dispatch_queue_create("con.itt.srq", DISPATCH_QUEUE_SERIAL);
    dispatch_async(serialQueue, ^{
        
        NSLog(@"task1");
    });
    dispatch_async(serialQueue, ^{
        
        NSLog(@"task2");
    });


(3)任务组

dispatch_queue_t myQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    
    dispatch_group_async(group, myQueue, ^{

        NSLog(@"task1");
    });
    dispatch_group_async(group, myQueue, ^{
        
        NSLog(@"task1");
    });
    dispatch_group_async(group, myQueue, ^{
        
        NSLog(@"task3");
    });
    
    NSLog(@"wait 1 2 3");
    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
    NSLog(@"task 1 2 3 finished");


(4)dispatch_group_enter、dispatch_group_leave和dispatch_group_wait来实现进程同步

//dispatch_group_enter和dispatch_group_leave必须成对出现
    dispatch_queue_t myQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_enter(group);
    
    dispatch_group_async(group, myQueue, ^{
        
        NSLog(@"again");
        
        sleep(5);
        
        dispatch_group_leave(group);
    });
    
    
    dispatch_group_enter(group);
    
    dispatch_group_async(group, myQueue, ^{
        
        NSLog(@"next");
        
        sleep(10);
        
        dispatch_group_leave(group);
    });
    
    NSLog(@"start");
    NSLog(@"waiting");
    
    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);//dispatch_group_wait表示等待任务组执行完毕
    
    NSLog(@"finish");


(5)信号量(控制并发)

实例一:

dispatch_group_t group = dispatch_group_create();
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(10);
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    for (int i = 0; i < 100; i++)
    {
        
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        dispatch_group_async(group, queue, ^{
            NSLog(@"%i",i);
            
            sleep(2);
            dispatch_semaphore_signal(semaphore);
        });
    }
    
    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
    
    
 实例二:
 
 
 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    dispatch_async(queue, ^{
    
        
        
        NSLog(@"waiting create data");
        
        sleep(5);
        
        NSLog(@"create data complete");
        
        dispatch_semaphore_signal(semaphore);
    });
    
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    dispatch_async(queue, ^{
        
        
        
        NSLog(@"haha~,I am get data complete");
        
    });

实例一程序运行会先创建10个新的线程,之后每当有线程执行结束,都会创建一个线程;实例二5秒后输出“haha~,I am get data complete”。

你可能感兴趣的:(IOS、多线程、GCD)