Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
多執行緒編程
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
MarkFly
April 16, 2019
Programming
90
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
多執行緒編程
MarkFly
April 16, 2019
Transcript
17 slides
Parallel Programming
Serial Queues vs Concurrent Queues
SERIAL QUEUES • 同⼀時間只執⾏⼀個任務 • 保證對共享資源的順序訪問,避免競爭條件 串列佇列
CONCURRENT QUEUES • 同⼀時間會執⾏多個任務 • 它們啟動的順序就是它們加入隊列的順序 • 執⾏時間和完成時間是不盡相同的 • 無法得知執⾏時間或者某⼀時刻被執⾏的任務數
並列佇列
Synchronous vs ASynchronous
SYNCHRONOUS • 不是⼀起執⾏的意思喔 • 任務加到隊列後,等到任務完成,線程才會返回繼續執⾏ • 所以會塞⾞ 同步執⾏
ASYNCHRONOUS • 反正跟上⼀個相反就對了 異步執⾏
GCD vs NSOperationQueue
GCD • Grand Central Dispatch • 以前語法 Like C, 但在Swift3語法有所改變
• init時要給個string當作識別⽤ • ⽤ .sync{} 或 .async{} 決定同步或異步 • 延遲執⾏⽤ .asyncAfter{}
GCD • qos: .userInteractive > userInitiated > default > utility
> background > unspecified • attributes: 都不給就是serial, 也可設為concurrent或initiallyInactive • 不⽤實例也可以, 如: DispatchQueue.main.async{}
你⼀⾔我⼀語?
NSOperationQueue • 把NSOperation放到NSOperationQueue裡⾯去執⾏(採concurrent) • 可以把NSOperationQueue暫停(把.isSuspended改為true), 但執⾏中 的任務不受影響 • NSOperation的⼦類NSBlockOperation 跟
NSInvocationOperation • NSOperationQueuePriority: > userInitiated > default > utility > background > unspecified • 可為任務添加依賴性addDependency(誰做完才做誰) • .dependencies列出該任務依賴的對象
NSOperationQueue • 可以把整個隊列上所有的任務都取消, 或只取消某個任務, 當call cancel()時, 可 能有三種情況 • 還沒執⾏:輪到該任務時將不會被執⾏
• 執⾏中:系統不會強制終⽌, 但會把isCancelled改為true • 已執⾏完成:什麼也不做 • 任務的狀態 • .isReady:取決於是否相依的任務是否已完成 • .isExecuting • .isFinished:執⾏completionBlock且移出隊列 • .isCancelled:被依賴的任務還是會執⾏
會造成什麼問題? • 競爭條件(Race Condition) 就是多個線程同時搶奪同⼀個資源, 導致數據與預期不同 • 優先倒置(Priority Inversion) 就是優先權較低的任務佔住了資源,
導致優先權較⾼的任務反⽽比較晚執⾏ • 死鎖問題(Dead Lock) 就是兩個任務互相等待彼此的資源, 導致兩個任務都無法結束(有點像retain cycle) • 活鎖問題(Live Lock) 就是死鎖的特例, 資源的狀態不斷改變, 雖有釋放, 但還是沒有進展
讓Xcode助你⼀臂之⼒
參考資料 • Swift 3學習指南:重新認識GCD應⽤ https://www.appcoda.com/grand-central-dispatch/ 16/08/11 • iOS 並⾏程式設計: 初探
NSOperation 和 Dispatch Queues https://www.appcoda.com.tw/ios-concurrency/ 15/12/09 • 平⾏程式設計可能會帶來甚麼問題? https://www.appcoda.com.tw/parallel-programming/ 18/06/26 • iOS⾯試之道第四章第四節
謝騰⾶ iOS Developer
More Decks by MarkFly
See All by MarkFly
關於 Mobile Developer 用 Flutter 去寫 Web 這檔事
mark33699
0
55
關於我在 visionOS 上 寫影片貼圖 app 這檔事
mark33699
0
320
iOS、Android、Flutter超級比一比
mark33699
1
1k
如何使用Swift遙控你的樂高
mark33699
0
76
直播簡單講
mark33699
1
130
演算法到底是在演哪一齣
mark33699
0
100
對戰三平棋
mark33699
0
210
Other Decks in Programming
See All in Programming
Going Full Circle with Virtual Graphs and Relational Databases
michaelsimons
1
120
高速開発を止めずに 性能を上げる Flyleのシステム刷新と改善の歴史
azihsoyn
0
110
Canvas 2D Context Next
petamoriken
0
930
Debian JP Projectのサーバー構成
uwabami
1
190
Crash Auto Pipelines
veronikapj
1
460
特急いなほ as a Workflow / 20261107jaws-festa-2026-akita-lt
kasacchiful
PRO
0
110
Spec-driven Development with AI
simas
PRO
0
2.3k
テスト駆動開発から学ぶ 達人プログラマーの型 / TechRAMEN 2026 Conference
a_okui
0
3.2k
仕様駆動開発入門 (何を作るかを先に決めて渡す) | 目指せ Kiro マスター 05
inariku
PRO
3
270
Claude Codeのおかげでやりたいことがやれるようになったんだよォー!
sms_tech
0
190
Compiling Your Language to WasmGC & Friends
tanishiking
0
960
ruby/uri meets WHATWG
jinroq
0
190
Featured
See All Featured
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
70
69k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.4k
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
Navigating Weather and Climate Data
rabernat
0
610
A Soul's Torment
seathinner
9
4k
Facilitating Awesome Meetings
lara
57
7.2k
What the history of the web can teach us about the future of AI
inesmontani
PRO
3
840
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
500
A better future with KSS
kneath
240
18k
Faster Mobile Websites
deanohume
310
32k
Practical Orchestrator
shlominoach
192
12k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.4k