Swift - 多线程实现方式1 - Thread

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

2,本文着重介绍Thread
Tread在三种多线程技术中是最轻量级的,但需要自己管理线程的生命周期和线程同步。线程同步对数据的加锁会有一定的系统开销。(本文代码已全部更新至Swift3)
3,Thread的两种创建方式
(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
28
29
import UIKit
class ViewController : UIViewController {
override func viewDidLoad() {
super .viewDidLoad()
//方式1:使用类方法
Thread .detachNewThreadSelector(#selector( .downloadImage),
toTarget: self ,with: nil )
//方式2:实例方法-便利构造器
let myThread = (target: ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
selector: #selector( ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
object: )
myThread.start()
}
//定义一个下载图片方法,线程调用
downloadImage(){
data = try! Data (contentsOf: URL (string: imageUrl)!)
print (data.count)
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
}

4,线程同步 线程同步方法通过锁来实现,每个线程都只用一个锁,这个锁与一个特定的线程关联。下面演示两个线程之间的同步。
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
68
//定义两个线程
var thread1: ?
thread2: ?
//定义两个线程条件,用于锁住线程
condition1 = NSCondition ()
condition2 = ()
viewDidLoad() {
.viewDidLoad()
thread2 = ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,selector: #selector( .method2),
)
thread1 = .method1),
)
thread1?.start()
}
//定义两方法,用于两个线程调用
method1(sender: AnyObject ){
for i in 0 ..< 10 {
( "Thread 1 running \(i)" )
sleep(1)
if i == 2 {
thread2?.start() //启动线程2
//本线程(thread1)锁定
condition1.lock()
condition1.wait()
condition1.unlock()
}
}
"Thread 1 over" )
//线程2激活
condition2.signal()
}
//方法2
method2(sender: AnyObject ){
0 ..< 10 {
"Thread 2 running \(i)" )
sleep(1)
i == 2 {
//线程1激活
condition1.signal()
//本线程(thread2)锁定
condition2.lock()
condition2.wait()
condition2.unlock()
}
}
"Thread 2 over" )
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
}
输出结果:

原文出自: www.hangge.com 转载请保留原文链接 http://www.hangge.com/blog/cache/detail_743.html

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...