标签:
//第四种线程创建方式
//创建了一个线程队列
// NSOperationQueue *q = [[NSOperationQueue alloc] init];
// q.maxConcurrentOperationCount = 3;//设置最大线程并发数
// //创建线程
// NSInvocationOperation *opreation1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(doOperation1) object:nil];//
// NSInvocationOperation *opreation2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(doThread1:) object:nil];//
// NSInvocationOperation *opreation3 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(doThread2:) object:nil];//
//
// [opreation2 setQueuePriority:NSOperationQueuePriorityVeryHigh];//设置线程的优先级
// [opreation3 setQueuePriority:NSOperationQueuePriorityVeryLow];
//
// [q addOperation:opreation1];
// [q addOperation:opreation2];
// [q addOperation:opreation3];
//第五种线程的创建方式
// dispatch_sync(<#dispatch_queue_t queue#>, <#^(void)block#>)同步事件
// dispatch_async(<#dispatch_queue_t queue#>, <#^(void)block#>)异步事件
// dispatch_queue_create(<#const char *label#>, <#dispatch_queue_attr_t attr#>)创建线程队列
// dispatch_release(<#dispatch_object_t object#>)//非ARC的线程队列的内存管理
// dispatch_get_main_queue()获取主队列
// dispatch_get_global_queue(<#long identifier#>, <#unsigned long flags#>)获取全局队列
// dispatch_async(dispatch_get_global_queue(0, 0), ^{
// NSLog(@"something %@",[NSThread currentThread]);
// });
// static dispatch_once_t one;
// dispatch_once(&one, ^{
// NSLog(@"onece %@",[NSThread currentThread]);
// });
//延迟执行
// double time = 2.0;
// //NSEC_PER_SEC表示系统的“秒”
// dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, time * NSEC_PER_SEC);
// dispatch_after(popTime, dispatch_get_main_queue(), ^{
// //
// });
//
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// //
// });
//线程队列事件的汇总
// dispatch_group_t group = dispatch_group_create();
// dispatch_group_async(group, <#dispatch_queue_t queue#>, <#^(void)block#>)
// dispatch_group_notify(group, <#dispatch_queue_t queue#>, <#^(void)block#>)
self.queue = [[NSOperationQueue alloc]init];
NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"*1**download image %@",[NSThread currentThread]);
}];
NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"*2***updata image %@",[NSThread currentThread]);
}];
NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"*3****save image %@",[NSThread currentThread]);
}];
NSBlockOperation *op4 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"*4*****show image %@",[NSThread currentThread]);
}];
//设定执行顺序,dependency依赖,可以跨队列
[op2 addDependency:op1];
[op3 addDependency:op2];
[op4 addDependency:op3];
[self.queue addOperation:op1];
[self.queue addOperation:op2];
[self.queue addOperation:op3];
[[NSOperationQueue mainQueue] addOperation:op4];
[self.queue addOperationWithBlock:^{
//
}];
// __block int a;
// int (^myBlock)(int,int)=^(int a,int b){ return a+b; };
}
-(void)doOperation1
{
NSLog(@"doOperation1:%@",[NSThread currentThread]);
[NSThread isMainThread];//判断是不是主线程
[NSThread isMultiThreaded];//判断是不是子线程
}
-(void)doThread1:(id)sender
{
NSLog(@"doThread1:%@",[NSThread currentThread]);
}
-(void)doThread2:(id)sender
{
NSLog(@"doThread2:%@ %@",[NSThread currentThread],sender);
}
标签:
原文地址:http://www.cnblogs.com/ningguozhu/p/5035985.html