nsoperation封装_IOS NSOperationQueue(线程 封装操作)

#import "HMViewController.h"

@interfaceHMViewController ()@end

@implementationHMViewController- (void)viewDidLoad

{

[super viewDidLoad];

[self operationQueue2];

}- (void)operationQueue2

{

NSBlockOperation*operation1 = [NSBlockOperation blockOperationWithBlock:^{

NSLog(@"NSBlockOperation------下载图片1---%@", [NSThread currentThread]);

}];

NSBlockOperation*operation2 = [NSBlockOperation blockOperationWithBlock:^{

NSLog(@"NSBlockOperation------下载图片2---%@", [NSThread currentThread]);

}];

[operation2 addExecutionBlock:^{

NSLog(@"NSBlockOperation------下载图片22---%@", [NSThread currentThread]);

}];

NSOperationQueue*queue =[[NSOperationQueue alloc] init];

queue.maxConcurrentOperationCount= 2;

[queue addOperation:operation1];

[queue addOperation:operation2];

[queue addOperationWithBlock:^{

NSLog(@"NSBlockOperation------下载图片3---%@", [NSThread currentThread]);

}];

}- (void)opeationListen

{

NSBlockOperation*operation = [NSBlockOperation blockOperationWithBlock:^{for (int i = 0; i<10; i++) {

NSLog(@"NSBlockOperation------下载图片---%@", [NSThread currentThread]);

}

}];

operation.completionBlock= ^{//...下载完图片后想做事情

NSLog(@"NSBlockOperation------下载图片完毕---%@", [NSThread currentThread]);

};

NSOperationQueue*queue =[[NSOperationQueue alloc] init];

[queue addOperation:operation];

}- (void)operationQueue

{//1.封装操作

NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download) object:nil];//operation1.queuePriority = NSOperationQueuePriorityHigh

NSInvocationOperation*operation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run) object:nil];

NSBlockOperation*operation3 = [NSBlockOperation blockOperationWithBlock:^{for (int i = 0; i < 10; i++) {

NSLog(@"NSBlockOperation------下载图片---%@", [NSThread currentThread]);

[NSThread sleepForTimeInterval:0.1];

}

}];//[operation3 addExecutionBlock:^{//for (int i = 0; i<10; i++) {//NSLog(@"NSBlockOperation------下载图片2---%@", [NSThread currentThread]);//[NSThread sleepForTimeInterval:0.1];//}//}];//2.创建队列

NSOperationQueue *queue =[[NSOperationQueue alloc] init];

queue.maxConcurrentOperationCount= 2; //2 ~ 3为宜//设置依赖

[operation2 addDependency:operation3];

[operation3 addDependency:operation1];//3.添加操作到队列中(自动执行操作, 自动开启线程)

[queue addOperation:operation1];

[queue addOperation:operation2];

[queue addOperation:operation3];//[queue setSuspended:YES];

}- (void)blockOperation

{//1.封装操作//NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{//NSLog(@"NSBlockOperation------下载图片1---%@", [NSThread currentThread]);//}];

NSBlockOperation*operation =[[NSBlockOperation alloc] init];

[operation addExecutionBlock:^{

NSLog(@"NSBlockOperation------下载图片1---%@", [NSThread currentThread]);

}];

[operation addExecutionBlock:^{

NSLog(@"NSBlockOperation------下载图片2---%@", [NSThread currentThread]);

}];

[operation addExecutionBlock:^{

NSLog(@"NSBlockOperation------下载图片3---%@", [NSThread currentThread]);

}];//2.执行操作

[operation start];

}- (void)invocationOperation

{//1.创建操作对象, 封装要执行的任务

NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download) object:nil];//2.执行操作(默认情况下, 如果操作没有放到队列queue中, 都是同步执行)

[operation start];

}- (void)download

{for (int i = 0; i<10; i++) {

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

[NSThread sleepForTimeInterval:0.1];

}

}- (void)run

{for (int i = 0; i<10; i++) {

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

[NSThread sleepForTimeInterval:0.1];

}

}@end

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值