多线程NSInvocationOperation(NSOperationQueue)的基本用法
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
/*---------------------------NSOperation--------------------------------------*/
//创建线程队列(线程池)
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
//关闭暂停队列
[queue setSuspended:YES];
//设置最大并发数
queue.maxConcurrentOperationCount = 1;
//创建线程
NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(thread1:) object:@"op1"];
//设置线程的优先级
operation1.queuePriority = NSOperationQueuePriorityNormal;
NSInvocationOperation *operation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(thread2:) object:@"op2"];
//设置优先级
operation2.queuePriority = NSOperationQueuePriorityHigh;
//将线程添加到队列中
[queue addOperation:operation1];
[queue addOperation:operation2];
//方式二:block
[queue addOperationWithBlock:^{
@autoreleasepool {
for (int i=0; i<50; i++) {
NSLog(@"op3:%d",i);
}
}
}];
}
- (void)thread1:(NSString *)threadName {
@autoreleasepool {
for (int i=0; i<50; i++) {
NSLog(@"thread1:%d",i);
}
}
}
- (void)thread2:(NSString *)threadName {
@autoreleasepool {
for (int i=0; i<50; i++) {
NSLog(@"thread2:%d",i);
}
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];
// [[NSRunLoop currentRunLoop] run];
}
}
- (void)timeAction {
NSLog(@"timeAction");
}
转载于:https://www.cnblogs.com/chillytao-suiyuan/p/4834130.html
多线程NSInvocationOperation(NSOperationQueue)的基本用法相关推荐
- python schedule多线程_Python定时任务sched模块用法示例
本文实例讲述了Python定时任务sched模块用法.分享给大家供大家参考,具体如下: 通过sched模块可以实现通过自定义时间,自定义函数,自定义优先级来执行函数. 范例一 import time ...
- python多线程:Thread类的用法
我们要创建Thread对象,然后让他们运行,每个Thread对象代表一个线程,在每个线程中我们可以让程序处理不同的任务,这就是多线程编程. 创建Thread对象有两种方法: 1.直接创建Thread, ...
- 多线程编程CreateThread和_beginthreadex用法
多线程编程 1.CreateThread HANDLE WINAPI CreateThread( LPSECURITY_ATTRIBUTES lpThreadAttributes, //内核对象的安全 ...
- 多线程——Condition的介绍以及用法
之前总结了wait和notify的用法,今天总结一下condition的用法. Condition介绍: 关键字synchronize可以与wait()和nitify()方法相结合实现实现等待/通知模 ...
- 多线程中,NSOperationQueue和GCD的区别
1.效率肯定是delegate比nsnotification高. 2. delegate方法比notification更加直接,最典型的特征是,delegate方法往往需要关注返回值, 也就是dele ...
- 多线程——Thread类的基本用法
一.线程创建 创建线程最常用的方法是使用lambda表达式 public class Thread {public static void main(String[] args) {Thread t ...
- NSOperationQueue线程队列完毕finished状态检测
参考: http://stackoverflow.com/questions/1049001/get-notification-when-nsoperationqueue-finishes-all-t ...
- iOS多线程全套:线程生命周期,多线程的四种解决方案,线程安全问题,GCD的使用,NSOperation的使用(下)
2017-07-08 remember17 Cocoa开发者社区 7NSOperation的理解与使用 No.1:NSOperation简介 NSOperation是基于GCD之上的更高一层封装,NS ...
- operation 多线程
2.Cocoa Operation 优点:不需要关心线程管理,数据同步的事情. Cocoa Operation 相关的类是 NSOperation ,NSOperationQueue.NSOpera ...
- 学习多线程 - iOS
实现多线程的方案一共有四种:pthread,NSThread,GCD,NSOperation 一.线程的状态与生命周期 新建:实例化线程对象 就绪:向线程对象发送start消息,线程对象就被加入到可调 ...
最新文章
- graphpad做饼图_如此简单的饼图,这些点你可能还不知道
- .net core EPPlus npoi_微软官方上线免费C#、.NET在线教程视频
- 什么是 SAP HCEA
- javascript第三天(2)
- 抽象工厂模式(Absraact Factory)介绍与实现
- c语言中数组名可以与其他变量名相同,C语言初学者入门讲座 第九讲 数组(1)...
- 2015/8/18 Python基本使用(2)
- 2018CES智能硬件与新技术盘点
- springboot中使用servlet通过配置类
- 二进制的科学计数法?白话谈谈计算机如何存储与理解小数:IEEE 754
- python三大器_Python 入门之 Python三大器 之 迭代器
- 微软sql服务器可以卸载,完美卸载SQL Server 2008的方法
- RadASM资源链接错误RC2104及找不到resource.h解决方案
- 大容量网盘才是王道?看看坚果云这类的小容量网盘的生存之道
- Unity3D案例太空射击(Space Shooter)流程介绍与代码分析(中)
- [附源码]Java计算机毕业设计SSM大学生志愿者管理系统
- Java 百度ocr文字识别-发票识别,并在页面显示信息
- 抖音创作者信用分:如何提升信用等级,获取更多机会
- HTML5 CSS3 生日快乐动画网页(粉红色的回忆)
- 快速入门丨篇五:如何进行运动控制器输入/输出IO的应用?
热门文章
- ERROR: function group_concat(character varying) does not exist
- 【Linux】五分钟搞定 Linux 文档全部知识,就看这篇文章
- 实例快速上手shell脚本
- 20200221_2_国家非文化遗产聚类分析
- 人工神经网络到底能干什么?到底在干什么?
- 虚拟机中运行windows内核
- 5.7 Universal Transformers
- linux:进程管理与SELinux
- python的作用域分别有几种_Python中作用域的深入讲解
- 力扣-606. 根据二叉树创建字符串