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
OOP2 - Multiprocessing
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Manatsawin Hanmongkolchai
May 08, 2014
Programming
170
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
150
gRPC load balancing with xDS
whs
0
1.1k
ArgoCD
whs
0
510
Writing Babel Plugin
whs
0
240
What's new in Cloud Next 2019
whs
0
340
A Date with gRPC
whs
1
1.5k
ตีแผ่ Microservice ด้วย Tracing
whs
0
430
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
IBM Bob Dojo #1 仕様駆動開発入門
oniak3ibm
PRO
0
390
Web Framework Benchmark Battle 2026, Any%
a_matsuda
2
2k
C#の現在地 進化の歴史と、AI時代の.NET Everywhere
neuecc
5
5.2k
UPDATE をやめる — EF Core でマスタをバージョン管理する
panda728
PRO
0
1.2k
golang-bob.pdf
iwatsukayura
0
130
見えないものを探る要求要件定義に必要な基本的思考 / invisible-requirement-thinking
minodriven
15
8.5k
月間100万人のサービス刷新で、 技術選定をHonoに振り切った話
yutobara
8
6.6k
IBM Bob でAI駆動開発『QC-Playbook』
ichirokano2
0
110
PumaからFalconに移行したRailsアプリケーションでの実践
falcon8823
0
170
すこし踏み込む CancellationToken
htkym
2
1.7k
Jetpack Compose Mechanisms
skydoves
3
820
JAWS-UG 東京支部が始める、JAWS-UG支部コラボ / JAWS-UG lunchtime LT Collaboration
y0hgi
0
270
Featured
See All Featured
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
380
Scaling GitHub
holman
464
140k
What the history of the web can teach us about the future of AI
inesmontani
PRO
2
740
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
930
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.4k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
18k
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
68
58k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.6k
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
480
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
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