GCD的简单封装

 扩展: dispatch_block_t :无参数block,使用起来很简单

 下载链接:http://pan.baidu.com/s/1bndN6Yb

   //串行队列
- (void)test1 {
  
    WJGCDQueue *queue = [[WJGCDQueue alloc]initSerial];
    [queue execute:^{
        NSLog(@"1--%@",[NSThread currentThread]);
    }];
    [queue execute:^{
        NSLog(@"2--%@",[NSThread currentThread]);
    }];
    [queue execute:^{
        NSLog(@"3--%@",[NSThread currentThread]);
    }];
}
    //并行队列
- (void)test2 {
    
    WJGCDQueue *queue = [[WJGCDQueue alloc]initConcurrent];
    [queue execute:^{
        NSLog(@"1--%@",[NSThread currentThread]);
    }];
    [queue execute:^{
        NSLog(@"2--%@",[NSThread currentThread]);
    }];
    [queue execute:^{
        NSLog(@"3--%@",[NSThread currentThread]);
    }];
}

   //复杂运算,更新ui
- (void)test3 {
    
    [WJGCDQueue executeInGlobalQueue:^{
        NSLog(@"处理业务逻辑");
        [WJGCDQueue executeInMainQueue:^{
            NSLog(@"更新UI");
        }];
    }];
}
    //延时操作
- (void)test4 {
    NSLog(@"%@",[NSDate date]);
    [WJGCDQueue executeInMainQueue:^{
         NSLog(@"%@",[NSDate date]);
    } afterDelaySecs:2];
}
    //定时器
- (void)test5 {
    self.gcdTimer = [[WJGCDTimer alloc]initInQueue:[WJGCDQueue mainQueue]];
    [self.gcdTimer event:^{
        NSLog(@"GCD定时器");
    } timeInterval:NSEC_PER_SEC];
    [self.gcdTimer start];
}
    //队列组
- (void)test6 {
    WJGCDGroup *group = [[WJGCDGroup alloc]init];
    WJGCDQueue *queue = [[WJGCDQueue alloc]initConcurrent];
    [queue execute:^{
        NSLog(@"线程1");
    } inGroup:group];
    [queue notify:^{
        NSLog(@"线程执行");
    } inGroup:group];
}

    //信号量
- (void)test7 {
    WJGCDSemaphore *semaphore = [[WJGCDSemaphore alloc]init];
    [semaphore signal];
    [semaphore wait];
}
posted @ 2015-07-23 01:52  幻想无极  阅读(260)  评论(0编辑  收藏  举报