Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
OOP2 - Multiprocessing
Search
Manatsawin Hanmongkolchai
May 08, 2014
Programming
160
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
OOP2 - Multiprocessing
Manatsawin Hanmongkolchai
May 08, 2014
More Decks by Manatsawin Hanmongkolchai
See All by Manatsawin Hanmongkolchai
Nix: Declarative OS
whs
0
130
gRPC load balancing with xDS
whs
0
1.1k
ArgoCD
whs
0
480
Writing Babel Plugin
whs
0
230
What's new in Cloud Next 2019
whs
0
330
A Date with gRPC
whs
1
1.5k
ตีแผ่ Microservice ด้วย Tracing
whs
0
410
Next Generation Smart Home
whs
0
1k
Istio and the Service Mesh Architecture
whs
3
1.1k
Other Decks in Programming
See All in Programming
Composerを使ったサプライチェーン攻撃の様子を眺めてみる #phpstudy
o0h
PRO
2
220
Webフレームワークの ベンチマークについて
yusukebe
0
130
AI駆動開発で崩れていくコードベースを立て直す
kyoko_nr_nr
1
440
TypeSpec で繋ぐ複数プロダクトの型安全
maroon8021
1
380
並列実装の現場、2ヶ月間実務でAIを使い倒したAIもPCも私も限界が近い
ming_ayami
0
110
メソッドのジェネリクスでGoの夢は広がるか? / Kyoto.go #65
utgwkk
3
560
ローカルLLMを使ってB2Bサービスを作っていての学び
yaotti
0
140
不変条件と整合性境界—ビジネスが決める設計判断と実現パターン / Invariants and Consistency Boundaries
nrslib
13
3.4k
Stage 3 Decorators でできること / できないこと / TSKaigi 2026
susisu
1
1.5k
Technical Debt: Understanding it Rightly, Engaging it Rightly #LaravelLiveJP
shogogg
0
190
Why Laravel apps break—Mastering the fundamentals to keep them maintainable
kentaroutakeda
1
340
GitHub Copilot CLIのいいところ
htkym
2
1.3k
Featured
See All Featured
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.3k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
580
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.2k
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
2
210
The SEO identity crisis: Don't let AI make you average
varn
0
480
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
180
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
250
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
600
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
118
120k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
320
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
400
Transcript
Thread • เหมือนเปิด Java 2 ตัวรันพร้อมกัน • แต่ใช้ตัวแปรร่วมกันได้ (เรียกว่า Shared
memory model)
Why threads • ข้อมูลจำนวนมากๆ • ทำอย่างอื่นรอกระบวนการที่ทำงานช้า (โหลดเว็บ, อ่านไฟล์, ประมวลผลเยอะ)
Race condition Start Balance > amount? Balace -= amount End
Yes No
Race condition Start Balance > amount? Balace -= amount End
Yes No Balance = 500 Amount = 500
Race condition Start Balance > amount? Balace -= amount End
Yes No Balance = 500
Race condition Start Balance > amount? Balace -= amount End
Yes No Balance = 0
Race condition Start Balance > amount? Balace -= amount End
Yes No Balance = 500 Amount = 500 Start Balance > amount? Balace -= amount End Yes No Thread 1 Thread 2
Race condition Start Balance = 500 Start Thread 1 Thread
2
Race condition Start Balance > amount? Yes Balance = 500
Start Thread 1 Thread 2
Race condition Start Balance > amount? Yes Start Balance >
amount? Balace -= amount End Yes Thread 1 Thread 2 Balance = 500
Race condition Start Balance > amount? Yes Start Balance >
amount? Balace -= amount End Yes Thread 1 Thread 2 Balance = 0
Race condition Start Balance > amount? Yes Balance = 0
Start Balance > amount? Balace -= amount End Yes No Thread 1 Thread 2
Race condition Start Balance > amount? Balace -= amount End
Yes Start Balance > amount? Balace -= amount End Yes No Thread 1 Thread 2 Balance = -500
Synchronized synchronized บังคับให้เข้าแถวเรียงหนึ่งก่อนเข้าทำงาน ใช้ในกรณีที่ต้องการใช้ตัวแปรร่วมกันหลายๆ เธรด งาน
Race condition Start Balance > amount? Balace -= amount End
Yes Balance = 0 Start Thread 1 Thread 2
Race condition Start Balance > amount? Balace -= amount End
Yes Balance = 0 Start Balance > amount? End No Thread 1 Thread 2
แบ่งงาน • Master-Slave • Divide and Conquer • อื่นๆ (อ่านใน
Gist เอา)
Master-Slave • ให้งานไป ไม่พอมาเอาเพิ่ม • Adder ใช้แบบนี้ 1 2 3
4 5 6 7 Thread 1 Thread 2 Thread 3
Master-Slave • ให้งานไป ไม่พอมาเอาเพิ่ม • Adder ใช้แบบนี้ 1 2 3
4 5 6 7 Thread 1 Thread 2 Thread 3
Master-Slave • ให้งานไป ไม่พอมาเอาเพิ่ม • Adder ใช้แบบนี้ 1 2 3
4 5 6 7 Thread 1 Thread 2 Thread 3
Master-Slave • ให้งานไป ไม่พอมาเอาเพิ่ม • Adder ใช้แบบนี้ 1 2 3
4 5 6 7 Thread 1 Thread 2 Thread 3
Divide and Conquer • ซอยปัญหาให้เล็กลงจนกว่าจะทำได้ • if(ปัญหาเล็กพอแล้ว){ หาคำตอบ; }else{ ซอยและแตกเธรดต่อ
}
Divide and Conquer Cake 1,000 g
Divide and Conquer Cake 250 g Cake 250 g Cake
250 g Cake 250 g
Divide and Conquer Cake 250 g Cake 250 g Cake
250 g Cake 250 g Cake 125 g Cake 125 g
Fork-Join • Fork = แบ่งงานกันไปทำ • Join = รวมงาน รายงาน
ระบบสุริยะฯ โลก ดวงอาทิตย์ ดาวพุธ ดาวศุกร์ งาน Fork สารบัญ ดวงอาทิตย์...........1 โลก…………….8 ดาวพุธ.................12 ดาวศุกร์...............15 Join
Join • หยุดรอจนกว่าเธรดนั้นจะทำงานเสร็จ ChildThread thread2 = new ChildThread(); thread2.start();
thread2.join(); System.out.println(thread2.getAnswer());
Interrupt • เวลา Join/Sleep อาจเกิด InterruptedException • เธรดอื่นสามารถ “ปลุก” เธรดที่
join/sleep อยู่ได้ โดยการสั่ง interrupt() • ใช้แจ้งให้ทำงานอื่นต่อ (เช่น อัพเดต % การทำงาน)
Thread pool • การ start thread ใหม่ใช้เวลา • Thread pool
คือการ recycle thread ที่ทำงานเสร็จ แล้วมาใช้งานใหม่ (คล้ายๆ master-slave)
ExecutorService • อยู่ใน java.util.concurrent.ExecutorService • ใช้จัดการคิวงาน: • ใส่งานลงในคิวด้วยคำสั่ง submit futuresList.add(eservice.submit(new
Task(index))); • จะได้คำตอบเป็นตัวแปรชนิด Future • ExecutorService จะจัดการการ start thread, การของานในคิวโดยอัตโนมัติ
ExecutorService • Future มีเมธอด get() เพื่อดึงคำตอบ (จะหยุดเธรด จนกว่าจะได้คำตอบ) • ต้องเก็บ
ArrayList ของ Future เอง
CompletionService • เหมือน ExecutorService • แต่เก็บ Future ให้อัตโนมัติ • มีเมธอด
take() รอจนกว่าจะมีงานเสร็จสดๆ ร้อนๆ แล้วคืน Future นั้นออกมา taskResult = cservice.take().get();
Callback pattern • ไม่ใช้ Future • บอกตัวแม่เมื่อทำงานเสร็จแล้ว // starting
task CallBackTask task = new CallBackTask(i); task.setCaller(this); // in task callBackTest.callBack(seq);
RecursiveTask ใช้เขียน Fork/Join class Fibonacci extends RecursiveTask<Integer> { public
final int n; public Fibonacci(int n) { this.n = n; } public Integer compute() { if (n <= 1) return n; Fibonacci f1 = new Fibonacci(n - 1); f1.fork(); Fibonacci f2 = new Fibonacci(n - 2); return f2.compute() + f1.join(); } } เหมือน run() ใน Runnable
Message passing • เป็นทางเลือกอื่นจาก Shared memory model • ใช้ตัวแปรร่วมกันไม่ได้ แต่ส่งข้อความหากันได้
• เหมาะสำหรับการประมวลผลบน Network
Message passing • เป็นทางเลือกอื่นจาก Shared memory model • ใช้ตัวแปรร่วมกันไม่ได้ แต่ส่งข้อความหากันได้
• เหมาะสำหรับการประมวลผลบน Network
MapReduce A ไทย 30 สังคม 20 B ไทย 40 สังคม
15 C ไทย 10 สังคม 10 D ไทย 20 สังคม 20
MapReduce A ไทย 30 สังคม 20 B ไทย 40 สังคม
15 C ไทย 10 สังคม 10 D ไทย 20 สังคม 20 Score = 20 Count = 1 Score = 15 Count = 1 Score = 10 Count = 1 Score = 20 Count = 1 Map
MapReduce A ไทย 30 สังคม 20 B ไทย 40 สังคม
15 C ไทย 10 สังคม 10 D ไทย 20 สังคม 20 Score = 20 Count = 1 Score = 15 Count = 1 Score = 10 Count = 1 Score = 20 Count = 1 Reduce Score = 35 Count = 2 Score = 30 Count = 2 Score = 65 Count = 4
MapReduce A ไทย 30 สังคม 20 B ไทย 40 สังคม
15 C ไทย 10 สังคม 10 D ไทย 20 สังคม 20 Subject=Social Score = 20 Count = 1 Subject=Social Score = 15 Count = 1 Subject=Social Score = 10 Count = 1 Subject=Social Score = 20 Count = 1 Subject=Thai Score = 30 Count = 1 Subject=Thai Score = 40 Count = 1 Subject=Thai Score = 10 Count = 1 Subject=Thai Score = 20 Count = 1
MapReduce Subject=Social Score = 35 Count = 2 Subject=Social Score
= 30 Count = 2 Subject=Thai Score = 70 Count = 2 Subject=Thai Score = 30 Count = 2
MapReduce Subject=Social Score = 65 Count = 4 Subject=Thai Score
= 100 Count = 4