GCD实现timer

uint64_tinterval =2*NSEC_PER_SEC;

//创建一个专门执行timer回调的GCD队列

dispatch_queue_tqueue =dispatch_queue_create("my queue",0);

//创建Timer

_timer=dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,0,0, queue);

//使用dispatch_source_set_timer函数设置timer参数

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

//设置回调

dispatch_source_set_event_handler(_timer, ^()

{

NSLog(@"Timer %@", [NSThreadcurrentThread]);

});

//dispatch_source默认是Suspended状态,通过dispatch_resume函数开始它

dispatch_resume(_timer);

你可能感兴趣的:(GCD实现timer)