swift语言的学习笔记五(线程)

Swift 并没有使用新一套线程,使用OC源有的一套线程。下面以例子来演示一下swift中使用线程。

其用包括常见的:NSThread,NSOperationQueue,GCG

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import UIKit  
  2.   
  3.   
  4. class swiftThreadDemo : UIViewController  
  5. {  
  6.     var queue = NSOperationQueue()  
  7. //    init()  
  8. //    {  
  9. //        //alloc  
  10. //        super.init()  
  11. //    }  
  12.       
  13.     deinit  
  14.     {  
  15.         //dealloc  
  16.     }  
  17.       
  18.     func testGCDThread()  
  19.     {  
  20.         dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {  
  21.   
  22.             //这里写需要大量时间的代码  
  23.   
  24.             for var i = 0; i < 100000; i++  
  25.             {  
  26.                 println("GCD thread running.")  
  27.             }  
  28.               
  29.             sleep(5);  
  30.   
  31.             dispatch_async(dispatch_get_main_queue(), {  
  32.   
  33.                 //这里返回主线程,写需要主线程执行的代码  
  34.                 println("这里返回主线程,写需要主线程执行的代码")  
  35.                 })  
  36.             })  
  37.     }  
  38.       
  39.     func testNSThread()  
  40.     {  
  41.         //方式一  
  42.         //NSThread.detachNewThreadSelector("threadInMainMethod:",toTarget:self,withObject:nil)  
  43.           
  44.         //方式二  
  45.         var myThread = NSThread(target:self,selector:"threadInMainMethod:",object:nil)  
  46.         myThread.start()  
  47.           
  48.     }  
  49.       
  50.     func threadInMainMethod(sender : AnyObject)  
  51.     {  
  52.         for var i = 0; i < 100000; i++  
  53.         {  
  54.             println("NSThread running.")  
  55.         }  
  56.           
  57.         sleep(5);  
  58.         println("NSThread over.")  
  59.     }  
  60.       
  61.     func testNSOperationQueue()  
  62.     {  
  63.         //func (op: NSOperation!)  
  64.         var mopt = myOperationThread()  
  65.         queue.addOperation(mopt)  
  66.     }  
  67. }  
  68.   
  69.   
  70. class myOperationThread : NSOperation  
  71. {  
  72.     override func start()  
  73.     {  
  74.         super.start()  
  75.     }  
  76.       
  77.     override func main()  
  78.     {  
  79.         for var i = 0; i < 100000; i++  
  80.         {  
  81.             println("NSOperation running.")  
  82.         }  
  83.         println("NSOperation over.")  
  84.     }  
  85. }  

调用:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. var st = swiftThreadDemo()  
  2.         st.testNSThread()  
  3.         sleep(2)  
  4.         st.testGCDThread()  
  5.           
  6.         st.testNSOperationQueue() 
0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值