亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

前言:

Guarded Suspension意為保護暫停,其核心思想是僅當(dāng)服務(wù)進程準(zhǔn)備好時,才提供服務(wù)。設(shè)想一種場景,服務(wù)器可能會在很短時間內(nèi)承受大量的客戶端請求,客戶端請求的數(shù)量可能超過服務(wù)器本身的即時處理能力,而服務(wù)端程序又不能丟棄任何一個客戶請求。此時,最佳的處理方案莫過于讓客戶端要求進行排隊,由服務(wù)端程序一個接一個處理。這樣,既保證了所有的客戶端請求均不丟失,同時也避免了服務(wù)器由于同時處理太多的請求而崩潰

1.Guarded Suspension模式的結(jié)構(gòu)

Guarded Suspension模式的主要成員有:Request、RequestQueueClientThread、 ServerThread

  • Request:表示客戶端請求
  • RequestQueue:用于保存客戶端請求隊列
  • ClientThread:客戶端進程
  • ServerThread:服務(wù)器進程

其中,ClientThread負(fù)責(zé)不斷發(fā)起請求,并將請求對象放入請求隊列。ServerThread則根據(jù)其自身的狀態(tài),在有能力處理請求時,從RequestQueue中提取請求對象加以處理。

從流程圖中可以看到,客戶端的請求數(shù)量超過了服務(wù)線程的能力。在頻繁的客戶端請求中,RequestQueue充當(dāng)了中間緩存,存放未處理的請求,保證了客戶請求不丟失,同時也保護了服務(wù)線程不會受到大量并發(fā)的請求,而導(dǎo)致計算機資源不足

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設(shè)計模式的文章就介紹到這了,更多相關(guān)Java多線程 Guarded Suspension內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解Java類加載機制中的雙親委派模型

    詳解Java類加載機制中的雙親委派模型

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

    Java try-catch-finally異常處理機制詳解

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

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

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

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

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

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

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

    SpringBoot+Mybatis使用Enum枚舉類型總是報錯No enum constant&n

    這篇文章主要介紹了SpringBoot+Mybatis使用Enum枚舉類型總是報錯No enum constant XX問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • java isInterrupted()判斷線程的實例講解

    java isInterrupted()判斷線程的實例講解

    在本篇內(nèi)容里小編給大家分享的是一篇關(guān)于java isInterrupted()判斷線程的實例講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。
    2021-05-05
  • 不使用他人jar包情況下優(yōu)雅的進行dubbo調(diào)用詳解

    不使用他人jar包情況下優(yōu)雅的進行dubbo調(diào)用詳解

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

    JAVA超級簡單的爬蟲實例講解

    下面小編就為大家?guī)硪黄狫AVA超級簡單的爬蟲實例講解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • 最新版?IDEA?2022.1?正式上線新功能一覽

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

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

最新評論