博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS NSOperationQueue(线程 封装操作)
阅读量:5010 次
发布时间:2019-06-12

本文共 4001 字,大约阅读时间需要 13 分钟。

 

 

#import "HMViewController.h"@interface HMViewController ()@end@implementation HMViewController- (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

 

转载于:https://www.cnblogs.com/liuwj/p/6604694.html

你可能感兴趣的文章
[转]Windows Shell 编程 第十一章 【来源:http://blog.csdn.net/wangqiulin123456/article/details/7987992】...
查看>>
修改presto新版源码让他支持redash数据库
查看>>
Javascript的书写位置
查看>>
树-线索二叉树
查看>>
JAVA遇见HTML——Servlet篇:Servlet基础
查看>>
第二章 Vue快速入门--20 品牌案例-完成品牌列表的添加功能+ 21 品牌案例-根据Id完成品牌的删除...
查看>>
Java单例模式
查看>>
重温WCF之消息契约(MessageContract)(六)
查看>>
Excel2007制作直方图和正态分布曲线图
查看>>
android adb常用指令
查看>>
Android框架之路——GreenDao3.2.2的使用
查看>>
类方法WCF学习笔记-KnowTypeAttribute用法
查看>>
平台程序微信平台开发应用的签名
查看>>
程序卡OK6410裸板更新程序_update
查看>>
MYSQL用户名:root
查看>>
JavaScript 开发规范要求
查看>>
Devstack 安装OpenStack Pike版本(单机环境)
查看>>
Javascript 函数初探
查看>>
类的定义、声明使用
查看>>
转载,gini系数代码对应的公式
查看>>