初探swift语言的学习笔记五(线程)
生活随笔
收集整理的這篇文章主要介紹了
初探swift语言的学习笔记五(线程)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
作者:fengsh998原文地址:http://blog.csdn.net/fengsh998/article/details/30354127轉(zhuǎn)載請注明出處假設認為文章對你有所幫助,請通過留言或關注微信公眾帳號fengsh998來支持我,謝謝!
調(diào)用:var st = swiftThreadDemo()st.testNSThread()sleep(2)st.testGCDThread()st.testNSOperationQueue()
swift 并沒有使用新一套線程,使用OC源有的一套線程。以下以樣例來演示一下swift中使用線程。
其用包含常見的:NSThread,NSOperationQueue,GCG
import UIKitclass swiftThreadDemo : UIViewController {var queue = NSOperationQueue() // init() // { // //alloc // super.init() // }deinit{//dealloc}func testGCDThread(){dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {//這里寫須要大量時間的代碼for var i = 0; i < 100000; i++{println("GCD thread running.")}sleep(5);dispatch_async(dispatch_get_main_queue(), {//這里返回主線程,寫須要主線程運行的代碼println("這里返回主線程,寫須要主線程運行的代碼")})})}func testNSThread(){//方式一//NSThread.detachNewThreadSelector("threadInMainMethod:",toTarget:self,withObject:nil)//方式二var myThread = NSThread(target:self,selector:"threadInMainMethod:",object:nil)myThread.start()}func threadInMainMethod(sender : AnyObject){for var i = 0; i < 100000; i++{println("NSThread running.")}sleep(5);println("NSThread over.")}func testNSOperationQueue(){//func (op: NSOperation!)var mopt = myOperationThread()queue.addOperation(mopt)} }class myOperationThread : NSOperation {override func start(){super.start()}override func main(){for var i = 0; i < 100000; i++{println("NSOperation running.")}println("NSOperation over.")} }調(diào)用:var st = swiftThreadDemo()st.testNSThread()sleep(2)st.testGCDThread()st.testNSOperationQueue()
總結(jié)
以上是生活随笔為你收集整理的初探swift语言的学习笔记五(线程)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android网络编程之使用HTTP訪问
- 下一篇: BP神经网络基本原理