欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java多線程 Guarded Suspension設(shè)計(jì)模式

 更新時(shí)間:2021年10月28日 11:46:50   作者:冬日毛毛雨  
這篇文章主要介紹了Java多線程 Guarded Suspension設(shè)計(jì)模式,Guarded Suspension意為保護(hù)暫停,其核心思想是僅當(dāng)服務(wù)進(jìn)程準(zhǔn)備好時(shí),才提供服務(wù),文章圍繞Java多線程 Guarded Suspension展開(kāi)內(nèi)容,需要的朋友可以參考一下

前言:

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類(lèi)加載機(jī)制中的雙親委派模型

    詳解Java類(lèi)加載機(jī)制中的雙親委派模型

    Java的雙親委派模型是一種類(lèi)加載機(jī)制,它用于保證Java類(lèi)的安全性和穩(wěn)定性,在這個(gè)模型中,當(dāng)一個(gè)類(lèi)需要被加載時(shí),Java虛擬機(jī)會(huì)先檢查自己是否已經(jīng)加載了該類(lèi),本文就給大家講解一下Java類(lèi)加載機(jī)制中的雙親委派模型,需要的朋友可以參考下
    2023-09-09
  • Java try-catch-finally異常處理機(jī)制詳解

    Java try-catch-finally異常處理機(jī)制詳解

    這篇文章主要介紹了Java try-catch-finally異常處理機(jī)制詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Java異常退出條件的判斷示例代碼

    Java異常退出條件的判斷示例代碼

    這篇文章主要介紹了Java異常退出條件的判斷示例代碼,涉及常見(jiàn)異常退出條件等相關(guān)內(nèi)容,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-10-10
  • 學(xué)習(xí)Java九大內(nèi)置對(duì)象

    學(xué)習(xí)Java九大內(nèi)置對(duì)象

    學(xué)習(xí)Java九大內(nèi)置對(duì)象,從現(xiàn)在開(kāi)始,希望大家可以通過(guò)這篇文章可以真正的理解Java九大內(nèi)置對(duì)象,感興趣的朋友可以參考一下
    2016-05-05
  • MyBatis中模糊查詢(xún)使用CONCAT('%',#{str},'%')出錯(cuò)的解決

    MyBatis中模糊查詢(xún)使用CONCAT('%',#{str},'%')出錯(cuò)的解

    這篇文章主要介紹了MyBatis中模糊查詢(xún)使用CONCAT('%',#{str},'%')出錯(cuò)的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • SpringBoot+Mybatis使用Enum枚舉類(lèi)型總是報(bào)錯(cuò)No enum constant XX問(wèn)題

    SpringBoot+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-12
  • java isInterrupted()判斷線程的實(shí)例講解

    java 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)用詳解

    這篇文章主要為大家介紹了不使用他人jar包情況下優(yōu)雅的進(jìn)行dubbo調(diào)用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • JAVA超級(jí)簡(jiǎn)單的爬蟲(chóng)實(shí)例講解

    JAVA超級(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
  • 最新版?IDEA?2022.1?正式上線新功能一覽

    最新版?IDEA?2022.1?正式上線新功能一覽

    4月12日,最新版的IDEA?2022.1正式發(fā)布,無(wú)論是從UI上,還是功能上,都有了很大的改進(jìn),完善,一起來(lái)看一下都有那些重要的更新
    2022-04-04

最新評(píng)論