前端之家收集整理的这篇文章主要介绍了
swift 多线程实现,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
//队列 同步 异步 锁 延时 依赖
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)
}