多线程-Thread


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

Thread的两种创建方式

(1)直接创建线程并且自动运行线程

(2)先创建一个线程对象,然后手动运行线程,在运行线程操作之前可以设置线程的优先级等线程信息。

1
Thread.detachNewThreadSelector(#selector(ViewController.downloadImage), toTarget: self, with: nil)
1
2
let myThread = Thread(target: self, selector: #selector(ViewController.downloadImage), object: nil)
myThread.start()
1
2
3
4
5
@objc func downloadImage() {
let imageUrl = "http://hangge.com/blog/images/logo.png"
let data = try! Data(contentsOf: URL(string: imageUrl)!)
print(data.count)
}

线程同步方法通过锁来实现,每个线程都只用一个锁,这个锁与一个特定的线程关联。下面演示两个线程之间的同步。

1
2
3
4
5
6
7
//定义两个线程
var thread1:Thread?
var thread2:Thread?

//定义两个线程条件,用于锁住线程
let condition1 = NSCondition()
let condition2 = NSCondition()
1
2
3
4
5
thread2 = Thread(target: self, selector: #selector(ViewController.method2),
object: nil)
thread1 = Thread(target: self, selector: #selector(ViewController.method1),
object: nil)
thread1?.start()
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
//定义两方法,用于两个线程调用
@objc func method1(sender:AnyObject){
for i in 0 ..< 10 {
print("Thread 1 running \(i)")
sleep(1)

if i == 2 {
thread2?.start() //启动线程2

//本线程(thread1)锁定
condition1.lock()
condition1.wait()
condition1.unlock()
}
}

print("Thread 1 over")

//线程2激活
condition2.signal()
}

//方法2
@objc func method2(sender:AnyObject){
for i in 0 ..< 10 {
print("Thread 2 running \(i)")
sleep(1)

if i == 2 {
//线程1激活
condition1.signal()

//本线程(thread2)锁定
condition2.lock()
condition2.wait()
condition2.unlock()
}
}

print("Thread 2 over")
}

基本思路是,首先要创建公用的NSCondition实例。然后:
消费者取得锁,取产品,如果没有,则wait,这时会释放锁,直到有线程唤醒它去消费产品;
生产者制造产品,首先也是要取得锁,然后生产,再发signal,这样可唤醒wait的消费者。