這篇文章主要介紹“Java多線程如何使用Guarded Suspension設計模式”,在日常操作中,相信很多人在Java多線程如何使用Guarded Suspension設計模式問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Java多線程如何使用Guarded Suspension設計模式”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
前言:
Guarded Suspension意為保護暫停,其核心思想是僅當服務進程準備好時,才提供服務。設想一種場景,服務器可能會在很短時間內承受大量的客戶端請求,客戶端請求的數量可能超過服務器本身的即時處理能力,而服務端程序又不能丟棄任何一個客戶請求。此時,最佳的處理方案莫過于讓客戶端要求進行排隊,由服務端程序一個接一個處理。這樣,既保證了所有的客戶端請求均不丟失,同時也避免了服務器由于同時處理太多的請求而崩潰
Guarded Suspension模式的主要成員有:Request、RequestQueue、ClientThread、 ServerThread
Request:表示客戶端請求
RequestQueue:用于保存客戶端請求隊列
ClientThread:客戶端進程
ServerThread:服務器進程
其中,ClientThread負責不斷發起請求,并將請求對象放入請求隊列。ServerThread則根據其自身的狀態,在有能力處理請求時,從RequestQueue中提取請求對象加以處理。
從流程圖中可以看到,客戶端的請求數量超過了服務線程的能力。在頻繁的客戶端請求中,RequestQueue充當了中間緩存,存放未處理的請求,保證了客戶請求不丟失,同時也保護了服務線程不會受到大量并發的請求,而導致計算機資源不足
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設計模式”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。