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
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
140
gRPC load balancing with xDS
whs
0
1.1k
ArgoCD
whs
0
500
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
420
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
初心者DevRelとして参加者だった私が、DevRel Talks!#2に登壇するまでにしてきたこと
sokohirai
0
340
『寄り添うラジオ』をAIで作る 体験価値から逆算した、会話しないUXと品質設計
theoriatec2024
3
130
tsc.rip を支える技術 / Kyoto.なんか #8
susisu
0
4.3k
thread_parallel_with_free-threaded_Python_and_NumPy.pdf
riku_sakamoto
0
150
Snowflakeで業務アプリを作ろう。 Snowflakeのアプリ機能解説&実践ガイド
ayumu_yamaguchi
1
150
Webプラットフォームで議論されているセキュリティ課題 / Security issues being discussed on Web Platforms
petamoriken
0
250
Vibes Containers 〜AIで変わるコンテナ設計と運用〜
tkikuc
3
510
AWS DevOps Agentで インシデント対応をAIに任せたい
honmarkhunt
7
2.6k
【デモ】Kiroで体験する仕様駆動開発|設計からコーディングまでAIと進める開発フロー
cmkudo
0
600
世界の中心で、AI(App Intents)をさけぶ ー App Intents中心設計の実践ガイド
touyou
0
170
Oxlintはいいぞ(続)
yug1224
1
560
Go × SIMDで高速化するベクトル検索 ~ルーフラインモデルでSIMDが効く境界を探れ! ~
po3rin
1
160
Featured
See All Featured
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.4k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6.1k
The SEO identity crisis: Don't let AI make you average
varn
0
550
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
2
560
sira's awesome portfolio website redesign presentation
elsirapls
0
410
Reality Check: Gamification 10 Years Later
codingconduct
0
2.3k
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
190
Side Projects
sachag
455
43k
Being A Developer After 40
akosma
91
590k
Practical Orchestrator
shlominoach
191
12k
Code Reviewing Like a Champion
maltzj
528
40k
Building a Scalable Design System with Sketch
lauravandoore
463
34k
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