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'}
}

最新文章

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

热门文章

  1. 7.使用dom4j实现增删改查
  2. 安装RabbitMQ服务器及基本配置
  3. 支付宝证书签名 PHP SDK
  4. C/C++ 多线程注意事项
  5. Python3解leetcode Rotate Array
  6. PHP缓存技术OB系统函数(总结)
  7. English-Words with 'ir'
  8. jmeter添加自定义扩展函数之String---base64加密
  9. 20141211--C# 构造函数
  10. 将QTP运行时的错误截图上传到QC