标签:
NSOperationQueue的简单使用过程:
1. 建立一个NSOperationQueue的对象
2. 建立一个NSOperation的对象
3. 将operation加入到NSOperationQueue中
MRC 需要手动释放
4. release掉operation
NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc]
initWithTarget:self
selector:nil)
object:nil];
[queue addOperation:operation];
/***********************************GCD**************************************************/
//一次性执行
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//code be executed once
});
//延迟调用
double delayInSeconds = 2.0;
dispatch_time_t timer = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds *NSEC_PER_SEC);
dispatch_after(timer, dispatch_get_main_queue(), ^{
//code be executed once after delay
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//code be executed once after delay
});
//自定义queque
dispatch_queue_t fun = dispatch_queue_create("funfun", NULL);
dispatch_async(fun, ^{
// your code
});
MRC 需要释放
//dispatch_release(fun);
//GCD组 同时进行多个任务,任务完成之后再主线程执行其他任务
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
//your code
});
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
//your code
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
//your code
});
//多线程的线程安全
[lock lock];
//your code
[lock unlock];
[lock lock];
//your code
[lock unlock];
});
标签:
原文地址:http://www.cnblogs.com/lhx2015/p/4630735.html