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];
}

转载于:https://www.cnblogs.com/hxwj/p/4669163.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值