swift 多线程实现
发布时间:2020-12-14 01:42:33 所属栏目:百科 来源:网络整理
导读://队列 同步 异步 锁 延时 依赖 func thread_queue(){ //创建串行队列 let serial_queue = dispatch_queue_create("label",DISPATCH_QUEUE_SERIAL) //创建并发队列 let con_queue = dispatch_queue_create("con",DISPATCH_QUEUE_CONCURRENT) //派送异步线程
//队列 同步 异步 锁 延时 依赖 func thread_queue(){ //创建串行队列 let serial_queue = dispatch_queue_create("label",DISPATCH_QUEUE_SERIAL) //创建并发队列 let con_queue = dispatch_queue_create("con",DISPATCH_QUEUE_CONCURRENT) //派送异步线程 dispatch_async(serial_queue,{ print("doing something 1") }) //派发同步线程 dispatch_sync(con_queue,{ print("doing something") }) //获得主线程队列 let main_queue = dispatch_get_main_queue() //创建队列组 let group = dispatch_group_create() //派发异步线程组 dispatch_group_async(group,main_queue,{ print("queue only have asynchronize method") }) } func thread_oop (){ // NSThread.detachNewThreadSelector("sel",toTarget: self,withObject: nil) // @convention() let mainThread = NSThread.mainThread() mainThread.performSelector("") mainThread.start() mainThread.cancel() mainThread.main() //abort() //exit(1) ///usage 1 let operation = NSBlockOperation(block: { }) ///usage 2 operation.addExecutionBlock({ }) operation.start() // operation.addDependency(<#T##op: NSOperation##NSOperation#>) let operation1 = NSOperation() operation1.addDependency(operation) //主线程负责页面刷新,主线程如果阻塞,程序将卡死. let queue = NSOperationQueue.mainQueue() queue.addOperations([operation1,operation],waitUntilFinished: false) } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |