前言:
Guarded Suspension
意為保護暫停,其核心思想是僅當服務進程準備好時,才提供服務。設想一種場景,服務器可能會在很短時間內承受大量的客戶端請求,客戶端請求的數量可能超過服務器本身的即時處理能力,而服務端程序又不能丟棄任何一個客戶請求。此時,最佳的處理方案莫過于讓客戶端要求進行排隊,由服務端程序一個接一個處理。這樣,既保證了所有的客戶端請求均不丟失,同時也避免了服務器由于同時處理太多的請求而崩潰
1.Guarded Suspension模式的結構
Guarded Suspension模式的主要成員有:Request
、RequestQueue
、ClientThread
、 ServerThread
-
Request
:表示客戶端請求 -
RequestQueue
:用于保存客戶端請求隊列 -
ClientThread
:客戶端進程 -
ServerThread
:服務器進程
其中,ClientThread
負責不斷發起請求,并將請求對象放入請求隊列。ServerThread
則根據其自身的狀態,在有能力處理請求時,從RequestQueue
中提取請求對象加以處理。
從流程圖中可以看到,客戶端的請求數量超過了服務線程的能力。在頻繁的客戶端請求中,RequestQueue
充當了中間緩存,存放未處理的請求,保證了客戶請求不丟失,同時也保護了服務線程不會受到大量并發的請求,而導致計算機資源不足
2. Guarded Suspension模式的簡單實現
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
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(); } } } } |
1
2
3
4
5
6
7
8
9
10
11
12
|
public class Request { private final String value; public Request(String value) { this .value = value; } public String getValue() { return value; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
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(); } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
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(); } } |
1
2
3
4
5
6
7
8
9
10
11
12
|
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內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://juejin.cn/post/7022609159912685575