Swift - 多线程实现方式(1) - NSThread

1,Swift继续使用Object-C原有的一套线程,包括三种多线程编程技术:
(1)NSThread
(2)Cocoa NSOperation(NSOperation和NSOperationQueue)
(3)Grand Central Dispath(GCD)

2,本文着重介绍NSThread
NSTread在三种多线程技术中是最轻量级的,但需要自己管理线程的生命周期和线程同步。线程同步对数据的加锁会有一定的系统开销。

3,NSThread的两种创建方式
(1)直接创建线程并且自动运行线程
(2)先创建一个线程对象,然后手动运行线程,在运行线程操作之前可以设置线程的优先级等线程信息。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import UIKit
 
class ViewController : UIViewController {
 
     override func viewDidLoad() {
         super .viewDidLoad()
         
         //方式1:使用类方法
         NSThread .detachNewThreadSelector( "downloadImage" , toTarget: self , withObject: nil )
         
         //方式2:实例方法-便利构造器
         var myThread: NSThread = NSThread (target: self , selector: "downloadImage" , object: nil )
         myThread.start()
     }
     
     //定义一个下载图片的方法,线程调用
     func downloadImage(){
         var imageUrl = "http://hangge.com/blog/images/logo.png"
         var data = NSData (contentsOfURL: NSURL (string: imageUrl)!, options: nil , error: nil )
         println (data?.length)
     }
     
     override func didReceiveMemoryWarning() {
         super .didReceiveMemoryWarning()
         // Dispose of any resources that can be recreated.
     }
}

4,线程同步
线程同步方法通过锁来实现,每个线程都只用一个锁,这个锁与一个特定的线程关联。下面演示两个线程之间的同步。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import UIKit
 
class ViewController : UIViewController {
     
     //定义两个线程
     var thread1: NSThread ?
     var thread2: NSThread ?
     
     //定义两个线程条件,用于锁住线程
     let condition1 = NSCondition ()
     let condition2 = NSCondition ()
 
     override func viewDidLoad() {
         super .viewDidLoad()
         
         thread2 = NSThread (target: self , selector: "method2:" , object: nil )
         thread1 = NSThread (target: self , selector: "method1:" , object: nil )
         thread1?.start()
     }
     
     //定义两方法,用于两个线程调用
     func method1(sender: AnyObject ){
         for var i=0; i<10; i++ {
             print ( "NSThread 1 running \(i)" )
             sleep(1)
             
             if i == 2 {
                 thread2?.start() //启动线程2
                 
                 //本线程(thread1)锁定
                 condition1.lock()
                 condition1.wait()
                 condition1.unlock()
             }
         }
             
         print ( "NSThread 1 over" )
         
         //线程2激活
         condition2.signal()
     }
     
     //方法2
     func method2(sender: AnyObject ){
         for var i=0; i<10; i++ {
             print ( "NSThread 2 running \(i)" )
             sleep(1)
             
             if i == 2 {
                 //线程1激活
                 condition1.signal()
                 
                 //本线程(thread2)锁定
                 condition2.lock()
                 condition2.wait()
                 condition2.unlock()
             }
         }
         
          print ( "NSThread 2 over" )
     }
     
     override func didReceiveMemoryWarning() {
         super .didReceiveMemoryWarning()
         // Dispose of any resources that can be recreated.
     }
}
输出结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
NSThread 1 running 0
NSThread 1 running 1
NSThread 1 running 2
NSThread 2 running 0
NSThread 2 running 1
NSThread 2 running 2
NSThread 1 running 3
NSThread 1 running 4
NSThread 1 running 5
NSThread 1 running 6
NSThread 1 running 7
NSThread 1 running 8
NSThread 1 running 9
NSThread 1 over
NSThread 2 running 3
NSThread 2 running 4
NSThread 2 running 5
NSThread 2 running 6
NSThread 2 running 7
NSThread 2 running 8
NSThread 2 running 9
NSThread 2 over
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值