如何使用线程Thread与Operation
发表于:2025-02-11 作者:千家信息网编辑
千家信息网最后更新 2025年02月11日,本篇内容主要讲解"如何使用线程Thread与Operation",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"如何使用线程Thread与Operation"
千家信息网最后更新 2025年02月11日如何使用线程Thread与Operation
本篇内容主要讲解"如何使用线程Thread与Operation",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"如何使用线程Thread与Operation"吧!
Thread
//Thread 原始方式//Thread.detachNewThread 快捷方式创建Threadfor i in 0...10 { Thread.detachNewThread{ print(i) }}/* 0 7 3 4 5 2 1 6 8 9 10 */
Thread.detachNewThreadSelector
//Thread.detachNewThreadSelectorclass ObjectForThread { func threadTest() { let thread = Thread(target: self, selector: #selector(threadWorker), object: nil) thread.start() print("threadTest") } @objc func threadWorker() { print("threadWorker") }}let obj = ObjectForThread()obj.threadTest()/* threadTest threadWorker */
Cocoa Operation
//Cocoa Operation(Operation和OperationQueue)class ObjectOperation { func test() { let operation = BlockOperation{ [weak self] in self?.threadWorker() } let queue = OperationQueue() queue.addOperation(operation) } @objc func threadWorker() { sleep(1) print("threadWorker") }}let objOperation = ObjectOperation()objOperation.test()print("after invoke test")//after invoke test//threadWorker
自定义Operation
//自定义Operationclass MyOperation: Operation { override func main() { sleep(1) print("MyOperation main") }}class CustomOperation { func test() { let operation = MyOperation() operation.completionBlock = { () -> Void in print("completionBlock") } let queue = OperationQueue() queue.addOperation(operation) }}let customOperation = CustomOperation()customOperation.test()print("after invoke customOperation test")//after invoke customOperation test//MyOperation main//completionBlock
到此,相信大家对"如何使用线程Thread与Operation"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
线程
内容
方式
学习
原始
实用
更深
兴趣
实用性
实际
操作简单
方法
更多
朋友
网站
频道
查询
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
美国高防服务器选择
怎样去掉网络安全上的勾
广州华博互联网科技有限公司
梦幻西游22年服务器
学校关于网络安全
无法连接服务器怎样弄
主键定位删除数据库
频繁的开关服务器耗电吗
制作网络安全书签内容
vs数据库中文为什么是问号
阿里云oss代替数据库
华为有招网络安全
物联网在网络安全方面的应用
天娃网络技术员招聘
服务器pc服务器
网络安全实名认证系统
服务器安全性是指什么
做电影用什么服务器
东营企业erp软件开发公司
上海新意网络软件开发
2u2路服务器是什么意思
APP软件开发公司那些较好
服务器安全请示报告
服务器繁忙怎么回事
魔兽世界 丽丽服务器
西城区网络营销软件开发概况
摩拜连不上服务器
数据库单词
新能科技互联网医院官网
mysql服务器性能选型