IOS-GCD中的Timer

- (IBAction)gcdResourceTestBtnPressed:(id)sender

{

    /// 初始化一个gcd队列.

    dispatch_queue_t timerQueue = dispatch_queue_create("timerQueue", 0);

    /// 创建 gcd timer.

    _timerSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, timerQueue);

    

    double interval = 2 * NSEC_PER_SEC; /// 间隔2秒

    dispatch_source_set_timer(_timerSource, dispatch_time(DISPATCH_TIME_NOW, 0), interval, 0);

    

    /// 定时器block设置

    dispatch_source_set_event_handler(_timerSource, ^{

        NSLog(@"%@",[NSThread currentThread]);

        progressTotal += 0.1;

        dispatch_async(dispatch_get_main_queue(), ^{

            [_progressView setProgress:progressTotal];

        });

    });

    /// 唤起定时器任务.

    dispatch_resume(_timerSource);

}


2014-11-22 10:21:50.964 MultitThreadManagement[856:410093] <NSThread: 0x7fd8d3511a50>{number = 2, name = (null)}

2014-11-22 10:21:52.969 MultitThreadManagement[856:410093] <NSThread: 0x7fd8d3511a50>{number = 2, name = (null)}

2014-11-22 10:21:54.969 MultitThreadManagement[856:410093] <NSThread: 0x7fd8d3511a50>{number = 2, name = (null)}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。