Guarded Suspension模式简单实现
2024-09-04 20:12:46
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'}
}
最新文章
- Redis-cluster集群【第四篇】:redis-cluster集群配置
- SqlServer2008R2附件数据库失败
- SQL union和union all的区别
- Android Studio常见问题 -- AndroidManifest.xml 覆盖问题
- Karaf 依赖equinox and felix,karaf 本Apache的很多项目作为基础框架
- 如何在java类中读取Properties配置文件
- T-SQL 控制流语句
- mac上charels抓包工具使用技巧
- POJ 2777 Count Color(段树)
- usaco 1.2.1(指针技巧)
- Sass与Compress实战:第四章
- keil mdk中如何确保某一段程序不被优化掉(转)
- mysql 分析5语句的优化--索引添加删除
- 从源码角度入手实现RecyclerView的Item点击事件
- MySQL (八)-- 事务、变量、触发器
- 使用CXF做简单的WebService例子
- nuxt Window 或 Document未定义解决方案
- 【一本通1248:Dungeon Master&;&;洛谷UVA532 Dungeon Master】
- 【.Net Core】Assets file project.assets.json not found. Run a NuGet package restore
- 多线程ExecutorService中submit和execute区别