-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewController.swift
78 lines (50 loc) · 1.95 KB
/
ViewController.swift
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
68
69
70
71
72
73
74
75
76
77
//
// ViewController.swift
// KatesOnlyOneThreadApp
//
// Created by KatesAndroid on 2021/1/28 AM 10:40
//
// 目標:指定如下作業僅能由同一執行緒完成。
// control one thread using semaphore 號誌。
// semaphore 可產生 signal 信號。
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// 產生多執行緒
// global 不放參處預設為 default
// 權限介於 UI 和 Backgound 之間
// 方法有非同步與同步兩種
DispatchQueue.global().async {
for i in 1 ... 20 {
// 號誌即信號燈
let s = DispatchSemaphore(value: 0)
if i == 5 {
DispatchQueue.main.async(execute:
{self.Alerter(ds: s)}
)
}else{
// 釋放信號
s.signal()
}
_ = s.wait(timeout: DispatchTime.distantFuture)
print(i)
sleep(1)
}
}
}
func Alerter(ds: DispatchSemaphore) {
// 提醒彈出
let showAlert = UIAlertController(title: "溫馨提醒", message: "您可點擊OK後繼續作業,完成任務。", preferredStyle: .alert)
// 提醒中的按鈕
let afterOK = UIAlertAction(title: "OK", style: .default){
(todo) in
ds.signal()
self.dismiss(animated: true, completion: nil)
}
// 為提醒加上按鈕
showAlert.addAction(afterOK)
// 彈出提醒
present(showAlert, animated: true)
}
}