SlideShare a Scribd company logo
Design Pattern
- Multi Thread -
2014/09/12
Winston Hsieh
Outline
 Thread Pool
 Future Mode
 Read-Write-Lock Mode
 Two-phase Termination Mode
2
Thread Pool Mode
 Thread pool 模式的概念就是,需要使用執行緒時,在一個執行
緒池中尋找可用的執行緒
 如果找不到再建立新的,執行緒使用完畢後,留在池中重複使用
 若能重複使用所建立的執行緒,而不是用完就丟,可以有效的重
複利用資源
3
Future Mode (1)
 考慮這樣一個情況,使用者可能快速翻頁瀏覽文件中,而圖片檔
案很大,如此在瀏覽到有圖片的頁數時,就會導致圖片的載入,
因而造成使用者瀏覽文件時會有停頓的現象
 所以我們希望在文件開啟之後,仍有一個背景作業持續載入圖片
,如此使用者在快速瀏覽頁面時,所造成的停頓可以獲得改善。
4
Future Mode
 在請求發生時,會先產生一個Future物件給發出請求的客戶
5
Future Mode
 而同時間,真正的目標物件之生成,由一個 新的執行緒持續進行
(即 Worker Thread),真正的目標物件生成之後,將之設定至
Future之中
6
Future Mode
 而當客戶端真正需要目標物件時, 目標物件也已經準備好,可以
讓客戶提取使用
7
Read-Write-Lock Mode (1)
 被讀取或寫入的資料一個鎖定物件,在讀取或寫入時要向鎖定物
件形式上讀取鎖定,實際上真正是否鎖定共用資源,由鎖定物件
來判斷。
8
public void readData() {
lock.readLock();
doRead();
lock.readUnLock();
}
public void writeData() {
lock.writeLock();
doWrite();
lock.writeUnLock();
}
Read-Write-Lock Mode (2) 9
 讀取者讀取資料時的Sequence Diagram
Read-Write-Lock Mode (3)
 如果可以同時讀取,現在假設有個讀取者已經取得鎖,另一個讀
取者其實也還是可以如下形式上取得鎖定並讀取
10
Read-Write-Lock Mode (4)
 如果現在只剩一個讀取者,而寫入者試圖進行寫入,它也試圖先
取得鎖定,但發現鎖已經被讀取的一方擁有,於是先進入等待,
直到讀取的一方解除鎖定為止。
11
Read-Write-Lock Mode (5)
 writerFirst是寫入優先的旗標,它確保只要有寫入的執行緒在等待
時,在解除鎖定的時候,可以優先由寫入執行緒取得鎖定,以確
保讀取者讀取到 的資料可以是最新的
12
private boolean writerFirst = true; // 寫入優先
public synchronized void readLock() {
try {
while(writingWriters > 0 || (writerFirst && waitingWriters >
0)) {
wait();
}
}
catch(InterruptedException e) {
e.printStackTrace();
}
readingReaders++;
}
public synchronized void readUnLock() {
readingReaders--;
writerFirst = true;
notifyAll();
}
public synchronized void writeLock() {
waitingWriters++
try {
while(readingReaders > 0 || writingWriters > 0) {
wait();
}
}
catch(InterruptedException e) {
e.printStackTrace();
}
finally {
waitingWriters--;
}
writingWriters++;
}
public synchronized void writeUnLock() {
writingWriters--;
writerFirst = false;
notifyAll();
}
Two-phase Termination Mode (1)
 所謂的兩階段終止 (Two-phase termination),即中止「運作階段
」,並完成「善後階段」,完整的完成執行緒的工作
 一個執行緒正在週期性的運作,在「運作階段」您送出了停止執
行緒的請求,這時候執行緒不該慌張的馬上終止目前的工作,而
是先完成這一次週期的工作,然後進入「善後階段」完成一些善
後的工作,例如 關閉檔案或網路串流
13
public class SomeThread extends Thread {
private boolean isTerminated = false;
public void terminate() {
isTerminated = true;
}
public void run() {
while(!isTerminated) {
// ... some statements
}
}
}
Two-phase Termination Mode (2) 14
public class SomeThread extends Thread {
private boolean isTerminated = false;
public void terminate() {
isTerminated = true;
interrupt();
}
public boolean isTerminated() {
return isTerminated;
}
private void doWorkBeforeShutdown() {
// .... do some work before shutdown
}
public void run() {
try {
while(!isTerminated) {
// ... some statements
}
}
catch(InterruptedException e) {
}
finally {
doWorkBeforeShutdown();
}
}
}
Thank You

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Design Pattern - Multi Thread

  • 1. Design Pattern - Multi Thread - 2014/09/12 Winston Hsieh
  • 2. Outline  Thread Pool  Future Mode  Read-Write-Lock Mode  Two-phase Termination Mode 2
  • 3. Thread Pool Mode  Thread pool 模式的概念就是,需要使用執行緒時,在一個執行 緒池中尋找可用的執行緒  如果找不到再建立新的,執行緒使用完畢後,留在池中重複使用  若能重複使用所建立的執行緒,而不是用完就丟,可以有效的重 複利用資源 3
  • 4. Future Mode (1)  考慮這樣一個情況,使用者可能快速翻頁瀏覽文件中,而圖片檔 案很大,如此在瀏覽到有圖片的頁數時,就會導致圖片的載入, 因而造成使用者瀏覽文件時會有停頓的現象  所以我們希望在文件開啟之後,仍有一個背景作業持續載入圖片 ,如此使用者在快速瀏覽頁面時,所造成的停頓可以獲得改善。 4
  • 6. Future Mode  而同時間,真正的目標物件之生成,由一個 新的執行緒持續進行 (即 Worker Thread),真正的目標物件生成之後,將之設定至 Future之中 6
  • 7. Future Mode  而當客戶端真正需要目標物件時, 目標物件也已經準備好,可以 讓客戶提取使用 7
  • 8. Read-Write-Lock Mode (1)  被讀取或寫入的資料一個鎖定物件,在讀取或寫入時要向鎖定物 件形式上讀取鎖定,實際上真正是否鎖定共用資源,由鎖定物件 來判斷。 8 public void readData() { lock.readLock(); doRead(); lock.readUnLock(); } public void writeData() { lock.writeLock(); doWrite(); lock.writeUnLock(); }
  • 9. Read-Write-Lock Mode (2) 9  讀取者讀取資料時的Sequence Diagram
  • 10. Read-Write-Lock Mode (3)  如果可以同時讀取,現在假設有個讀取者已經取得鎖,另一個讀 取者其實也還是可以如下形式上取得鎖定並讀取 10
  • 11. Read-Write-Lock Mode (4)  如果現在只剩一個讀取者,而寫入者試圖進行寫入,它也試圖先 取得鎖定,但發現鎖已經被讀取的一方擁有,於是先進入等待, 直到讀取的一方解除鎖定為止。 11
  • 12. Read-Write-Lock Mode (5)  writerFirst是寫入優先的旗標,它確保只要有寫入的執行緒在等待 時,在解除鎖定的時候,可以優先由寫入執行緒取得鎖定,以確 保讀取者讀取到 的資料可以是最新的 12 private boolean writerFirst = true; // 寫入優先 public synchronized void readLock() { try { while(writingWriters > 0 || (writerFirst && waitingWriters > 0)) { wait(); } } catch(InterruptedException e) { e.printStackTrace(); } readingReaders++; } public synchronized void readUnLock() { readingReaders--; writerFirst = true; notifyAll(); } public synchronized void writeLock() { waitingWriters++ try { while(readingReaders > 0 || writingWriters > 0) { wait(); } } catch(InterruptedException e) { e.printStackTrace(); } finally { waitingWriters--; } writingWriters++; } public synchronized void writeUnLock() { writingWriters--; writerFirst = false; notifyAll(); }
  • 13. Two-phase Termination Mode (1)  所謂的兩階段終止 (Two-phase termination),即中止「運作階段 」,並完成「善後階段」,完整的完成執行緒的工作  一個執行緒正在週期性的運作,在「運作階段」您送出了停止執 行緒的請求,這時候執行緒不該慌張的馬上終止目前的工作,而 是先完成這一次週期的工作,然後進入「善後階段」完成一些善 後的工作,例如 關閉檔案或網路串流 13 public class SomeThread extends Thread { private boolean isTerminated = false; public void terminate() { isTerminated = true; } public void run() { while(!isTerminated) { // ... some statements } } }
  • 14. Two-phase Termination Mode (2) 14 public class SomeThread extends Thread { private boolean isTerminated = false; public void terminate() { isTerminated = true; interrupt(); } public boolean isTerminated() { return isTerminated; } private void doWorkBeforeShutdown() { // .... do some work before shutdown } public void run() { try { while(!isTerminated) { // ... some statements } } catch(InterruptedException e) { } finally { doWorkBeforeShutdown(); } } }

Editor's Notes

  1. 1