Java多線程 Guarded Suspension設計模式

前言:

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

1.Guarded Suspension模式的結構

Guarded Suspension模式的主要成員有:RequestRequestQueueClientThreadServerThread

  • Request:表示客戶端請求
  • RequestQueue:用於保存客戶端請求隊列
  • ClientThread:客戶端進程
  • ServerThread:服務器進程

其中,ClientThread負責不斷發起請求,並將請求對象放入請求隊列。ServerThread則根據其自身的狀態,在有能力處理請求時,從RequestQueue中提取請求對象加以處理。

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

2. Guarded Suspension模式的簡單實現

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.

到此這篇關於Java多線程 Guarded Suspension設計模式的文章就介紹到這瞭,更多相關Java多線程 Guarded Suspension內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: