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ù)量可能超過(guò)服務(wù)器本身的即時(shí)處理能力,而服務(wù)端程序又不能丟棄任何一個(gè)客戶請(qǐng)求。此時(shí),最佳的處理方案莫過(guò)于讓客戶端要求進(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ù)量超過(guò)了服務(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)文章
Java try-catch-finally異常處理機(jī)制詳解
這篇文章主要介紹了Java try-catch-finally異常處理機(jī)制詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08學(xué)習(xí)Java九大內(nèi)置對(duì)象
學(xué)習(xí)Java九大內(nèi)置對(duì)象,從現(xiàn)在開(kāi)始,希望大家可以通過(guò)這篇文章可以真正的理解Java九大內(nèi)置對(duì)象,感興趣的朋友可以參考一下2016-05-05MyBatis中模糊查詢(xún)使用CONCAT('%',#{str},'%')出錯(cuò)的解
這篇文章主要介紹了MyBatis中模糊查詢(xún)使用CONCAT('%',#{str},'%')出錯(cuò)的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01SpringBoot+Mybatis使用Enum枚舉類(lèi)型總是報(bào)錯(cuò)No enum constant&n
這篇文章主要介紹了SpringBoot+Mybatis使用Enum枚舉類(lèi)型總是報(bào)錯(cuò)No enum constant XX問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12java isInterrupted()判斷線程的實(shí)例講解
在本篇內(nèi)容里小編給大家分享的是一篇關(guān)于java isInterrupted()判斷線程的實(shí)例講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-05-05不使用他人jar包情況下優(yōu)雅的進(jìn)行dubbo調(diào)用詳解
這篇文章主要為大家介紹了不使用他人jar包情況下優(yōu)雅的進(jìn)行dubbo調(diào)用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09JAVA超級(jí)簡(jiǎn)單的爬蟲(chóng)實(shí)例講解
下面小編就為大家?guī)?lái)一篇JAVA超級(jí)簡(jiǎn)單的爬蟲(chóng)實(shí)例講解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10