Guarded Suspension 意为保护暂停,假设服务器很短时间内承受大量的客户端请求,客户端请求的数量超过服务器本身的即时处理能力,而服务器又不能丢弃任何一个客户端请求,此时可以让客户端的请求进行排队,由服务端程序一个接一个处理,保证了所有的客户端请求均不丢失,同时避免了服务器由于同时处理太多的请求崩溃
主要角色:
- Request:客户端请求
- RequestQueue:客户端请求队列
- ClientThread: 客户端进程
- ServerThread: 服务器进程
/**
* 请求的内容
*/
public class Request {
private String name;
public Request(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Request{" +
"name='" + name + '\'' +
'}';
}
}
import java.util.LinkedList;
public class RequestQueue {
private LinkedList queue = new LinkedList();
public synchronized Request getRequest(){
while (queue.size()==0){
try {
wait(); //等待新的Request
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return (Request) queue.remove(); //返回Request队列中的第一个请求
}
public synchronized void addRequest(Request request){
queue.add(request); //添加Request请求
notifyAll(); //通知getRequest()
}
}
public class ClientThread extends Thread {
private RequestQueue requestQueue;
public ClientThread(RequestQueue requestQueue,String name) {
super(name);
this.requestQueue = requestQueue;
}
@Override
public void run() {
for (int i = 0; i < 3; i++) { //此次i<3为了输出少量结果
//构造请求
Request request = new Request("RequestId:" + i + " Thread_Name:" + Thread.currentThread().getName());
System.out.println(Thread.currentThread().getName()+" addRequest "+request);
requestQueue.addRequest(request); //提交请求
try {
Thread.sleep(10); //客户端耗时操作,速度快于服务端处理速度
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("ClientThread Name is:"+Thread.currentThread().getName());
}
System.out.println(Thread.currentThread().getName()+" 请求完成");
}
}
public class ServerThread extends Thread {
private RequestQueue requestQueue;
public ServerThread(RequestQueue requestQueue,String name) {
super(name);
this.requestQueue = requestQueue;
}
@Override
public void run() {
while (true){
final Request request = requestQueue.getRequest(); //得到请求
try {
Thread.sleep(100);//模拟请求耗时
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" handles "+request);
}
}
}
public class Main {
public static void main(String[] args){
RequestQueue requestQueue = new RequestQueue(); // 请求队列
for (int i = 0; i < 1; i++) {
new ServerThread(requestQueue,"ServerThread "+i).start();
}
for (int i = 0; i < 1; i++) {
new ClientThread(requestQueue,"ClientThread "+i).start();
}
}
//ClientThread 0 addRequest Request{name='RequestId:0 Thread_Name:ClientThread 0'}
//ClientThread Name is:ClientThread 0
//ClientThread 0 addRequest Request{name='RequestId:1 Thread_Name:ClientThread 0'}
//ClientThread Name is:ClientThread 0
//ClientThread 0 addRequest Request{name='RequestId:2 Thread_Name:ClientThread 0'}
//ClientThread Name is:ClientThread 0
//ClientThread 0 请求完成
//ServerThread 0 handles Request{name='RequestId:0 Thread_Name:ClientThread 0'}
//ServerThread 0 handles Request{name='RequestId:1 Thread_Name:ClientThread 0'}
//ServerThread 0 handles Request{name='RequestId:2 Thread_Name:ClientThread 0'}
}