Java多線程 Guarded Suspension設計模式
前言:
Guarded Suspension意為保護暫停,其核心思想是僅當服務進程準備好時,才提供服務。設想一種場景,服務器可能會在很短時間內(nèi)承受大量的客戶端請求,客戶端請求的數(shù)量可能超過服務器本身的即時處理能力,而服務端程序又不能丟棄任何一個客戶請求。此時,最佳的處理方案莫過于讓客戶端要求進行排隊,由服務端程序一個接一個處理。這樣,既保證了所有的客戶端請求均不丟失,同時也避免了服務器由于同時處理太多的請求而崩潰
1.Guarded Suspension模式的結(jié)構(gòu)
Guarded Suspension模式的主要成員有:Request、RequestQueue、ClientThread、 ServerThread
Request:表示客戶端請求RequestQueue:用于保存客戶端請求隊列ClientThread:客戶端進程ServerThread:服務器進程
其中,ClientThread負責不斷發(fā)起請求,并將請求對象放入請求隊列。ServerThread則根據(jù)其自身的狀態(tài),在有能力處理請求時,從RequestQueue中提取請求對象加以處理。
從流程圖中可以看到,客戶端的請求數(shù)量超過了服務線程的能力。在頻繁的客戶端請求中,RequestQueue充當了中間緩存,存放未處理的請求,保證了客戶請求不丟失,同時也保護了服務線程不會受到大量并發(fā)的請求,而導致計算機資源不足
2. Guarded Suspension模式的簡單實現(xiàn)
public class ClientThread extends Thread {
private final RequestQueue queue;
private final Random random;
private final String sendValue;
public ClientThread(RequestQueue queue, String sendValue) {
this.queue = queue;
this.sendValue = sendValue;
this.random = new Random(System.currentTimeMillis());
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("Client -> request " + sendValue);
queue.putRequest(new Request(sendValue));
try {
Thread.sleep(random.nextInt(1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Request {
private final String value;
public Request(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
public class RequestQueue {
private final LinkedList<Request> queue = new LinkedList<>();
public Request getRequest() {
synchronized (queue) {
while (queue.size() <= 0) {
try {
queue.wait();
} catch (InterruptedException e) {
return null;
}
}
return queue.removeFirst();
}
}
public void putRequest(Request request) {
synchronized (queue) {
queue.addLast(request);
queue.notifyAll();
}
}
}
public class ServerThread extends Thread {
private final RequestQueue queue;
private final Random random;
private volatile boolean closed = false;
public ServerThread(RequestQueue queue) {
this.queue = queue;
random = new Random(System.currentTimeMillis());
}
@Override
public void run() {
while (!closed) {
Request request = queue.getRequest();
if (null == request) {
System.out.println("Received the empty request.");
continue;
}
System.out.println("Server ->" + request.getValue());
try {
Thread.sleep(random.nextInt(1000));
} catch (InterruptedException e) {
return;
}
}
}
public void close() {
this.closed = true;
this.interrupt();
}
}
public class SuspensionClient {
public static void main(String[] args) throws InterruptedException {
final RequestQueue queue = new RequestQueue();
new ClientThread(queue,"Jack").start();
ServerThread serverThread = new ServerThread(queue);
serverThread.start();
Thread.sleep(10000);
serverThread.close();
}
}
運行:
Client -> request Jack
Server ->Jack
Client -> request Jack
Server ->Jack
Client -> request Jack
Server ->Jack
Client -> request Jack
Server ->Jack
Client -> request Jack
Client -> request Jack
Client -> request Jack
Server ->Jack
Client -> request Jack
Client -> request Jack
Server ->Jack
Client -> request Jack
Server ->Jack
Server ->Jack
Server ->Jack
Server ->Jack
Received the empty request.
到此這篇關(guān)于Java多線程 Guarded Suspension設計模式的文章就介紹到這了,更多相關(guān)Java多線程 Guarded Suspension內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java try-catch-finally異常處理機制詳解
這篇文章主要介紹了Java try-catch-finally異常處理機制詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08
MyBatis中模糊查詢使用CONCAT('%',#{str},'%')出錯的解
這篇文章主要介紹了MyBatis中模糊查詢使用CONCAT('%',#{str},'%')出錯的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
SpringBoot+Mybatis使用Enum枚舉類型總是報錯No enum constant&n
這篇文章主要介紹了SpringBoot+Mybatis使用Enum枚舉類型總是報錯No enum constant XX問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
不使用他人jar包情況下優(yōu)雅的進行dubbo調(diào)用詳解
這篇文章主要為大家介紹了不使用他人jar包情況下優(yōu)雅的進行dubbo調(diào)用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09

