Java多線程 Guarded Suspension設(shè)計(jì)模式
前言:
Guarded Suspension意為保護(hù)暫停,其核心思想是僅當(dāng)服務(wù)進(jìn)程準(zhǔn)備好時(shí),才提供服務(wù)。設(shè)想一種場(chǎng)景,服務(wù)器可能會(huì)在很短時(shí)間內(nèi)承受大量的客戶端請(qǐng)求,客戶端請(qǐng)求的數(shù)量可能超過服務(wù)器本身的即時(shí)處理能力,而服務(wù)端程序又不能丟棄任何一個(gè)客戶請(qǐng)求。此時(shí),最佳的處理方案莫過于讓客戶端要求進(jìn)行排隊(duì),由服務(wù)端程序一個(gè)接一個(gè)處理。這樣,既保證了所有的客戶端請(qǐng)求均不丟失,同時(shí)也避免了服務(wù)器由于同時(shí)處理太多的請(qǐng)求而崩潰
1.Guarded Suspension模式的結(jié)構(gòu)
Guarded Suspension模式的主要成員有:Request、RequestQueue、ClientThread、 ServerThread
Request:表示客戶端請(qǐng)求RequestQueue:用于保存客戶端請(qǐng)求隊(duì)列ClientThread:客戶端進(jìn)程ServerThread:服務(wù)器進(jìn)程
其中,ClientThread負(fù)責(zé)不斷發(fā)起請(qǐng)求,并將請(qǐng)求對(duì)象放入請(qǐng)求隊(duì)列。ServerThread則根據(jù)其自身的狀態(tài),在有能力處理請(qǐng)求時(shí),從RequestQueue中提取請(qǐng)求對(duì)象加以處理。
從流程圖中可以看到,客戶端的請(qǐng)求數(shù)量超過了服務(wù)線程的能力。在頻繁的客戶端請(qǐng)求中,RequestQueue充當(dāng)了中間緩存,存放未處理的請(qǐng)求,保證了客戶請(qǐng)求不丟失,同時(shí)也保護(hù)了服務(wù)線程不會(huì)受到大量并發(fā)的請(qǐng)求,而導(dǎo)致計(jì)算機(jī)資源不足
2. Guarded Suspension模式的簡(jiǎn)單實(shí)現(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();
}
}
運(yùn)行:
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設(shè)計(jì)模式的文章就介紹到這了,更多相關(guān)Java多線程 Guarded Suspension內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Sentinel自定義異常的三種實(shí)現(xiàn)方式
Spring Cloud Alibaba Sentinel 是目前主流并開源的流量控制和系統(tǒng)保護(hù)組件,Spring Cloud Alibaba Sentinel 有 3 種自定義異常的實(shí)現(xiàn)方式,本文小編將通過代碼示例給大家詳細(xì)的介紹這三種實(shí)現(xiàn)方式,需要的朋友可以參考下2023-11-11
Quarkus中ConfigSourceInterceptor的加密配置實(shí)現(xiàn)
這篇文章主要為大家介紹Quarkus中ConfigSourceInterceptor加密配置的實(shí)現(xiàn)方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-02-02
SpringBoot生成PDF的五種實(shí)現(xiàn)方法總結(jié)
這篇文章主要介紹了SpringBoot生成PDF的五種實(shí)現(xiàn)方法,在開發(fā)中經(jīng)常會(huì)遇到需要進(jìn)行對(duì)一些數(shù)據(jù)進(jìn)行動(dòng)態(tài)導(dǎo)出PDF文件,然后讓用戶自己選擇是否需要打印出來,這篇文章我們來介紹五種實(shí)現(xiàn)方法,需要的朋友可以參考下2024-10-10
將應(yīng)用程序進(jìn)行Spring6遷移的最佳使用方式
這篇文章主要介紹了將應(yīng)用程序進(jìn)行Spring6遷移的最佳方式,以及如何充分利用此升級(jí),需要的朋友可以參考下,如有錯(cuò)誤的地方還請(qǐng)指正2023-03-03
Java實(shí)現(xiàn)動(dòng)態(tài)驗(yàn)證碼生成
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)動(dòng)態(tài)驗(yàn)證碼生成,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04

