Guarded Suspension模式简单实现
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'}
}
Guarded Suspension模式简单实现的更多相关文章
- 多线程系列之四:Guarded Suspension 模式
一,什么是Guarded Suspension模式如果执行现在的处理会造成问题,就让执行处理的线程等待.这种模式通过让线程等待来保证实例的安全性 二,实现一个简单的线程间通信的例子 一个线程(Clie ...
- 并发设计模式之Guarded Suspension模式
- 原文链接: http://www.joyhwong.com/2016/11/19/并发设计模式之guarded-suspension模式/ Guarded Suspension意为保护暂停,其核心 ...
- 并行模式之Guarded Suspension模式
并行模式之Guarded Suspension模式 一).Guarded Suspension: 保护暂存模式 应用场景:当多个客户进程去请求服务进程时,客户进程的请求速度比服务进程处里请求的速度快, ...
- 多线程程序设计学习(4)guarded suspension模式
Guarded Suspension[生产消费者模式] 一:guarded suspension的参与者--->guardedObject(被防卫)参与者 1.1该 ...
- 多线程同步循环打印和Guarded suspension 模式
* 迅雷笔试题: * 有三个线程ID分别是A.B.C,请有多线编程实现,在屏幕上循环打印10次ABCABC… 由于线程执行的不确定性,要保证这样有序的输出,必须控制好多线程的同步. 线程同步有两种 ...
- 多线程学习之三生产者消费者模式Guarded Suspension
Guarded Suspension[生产消费者模式] 一:guarded suspension的参与者--->guardedObject(被防卫)参与者 1.1该 ...
- Guarded Suspension Pattern【其他模式】
Guarded Suspension Pattern public class GuardedSuspension { /** * Guarded Suspension Pattern[保护悬挂模式] ...
- Java设计模式之-----工厂模式(简单工厂,抽象工厂)
一.工厂模式主要是为创建对象提供过渡接口,以便将创建对象的具体过程屏蔽隔离起来,达到提高灵活性的目的. 工厂模式在<Java与模式>中分为三类:1)简单工厂模式(Simple Factor ...
- 分布式系统的消息&服务模式简单总结
分布式系统的消息&服务模式简单总结 在一个分布式系统中,有各种消息的处理,有各种服务模式,有同步异步,有高并发问题甚至应对高并发问题的Actor编程模型,本文尝试对这些问题做一个简单思考和总结 ...
随机推荐
- Python中的"Special Method"
The first thing to know about special methods is that they are meant to be called by the Python inte ...
- KEGG注释
在 KEGG 数据库中,把功能相似的蛋白质归为同一组,然后标上 KO 号.通过相似性比对,可以为未知功能的蛋白序列注释上 KO 号. 截止到 2015 年 6 月 12 日,KEGG 数据库中共收录了 ...
- Java反射及注解学习- 反射的使用 - JDK动态代理
代理模式基本概念:1.代理模式的作用:为其他对象提供一种以控制对方的访问在某种情况下,一个客户不想或者不能直接引用另一个对象,代理可以在客户端和目标对象之间起到中介的作用代理的角色:(1)抽象角色:声 ...
- spring-boot整合Mybatis多数据源案例
1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 2.GITHUB地址 https://github.com/nbfujx/springBo ...
- SQL_2008安装教程(完整版)
Win 7 win xp系统中SQL2008安装注意事项一:SQL2008 镜像下载地址 http://download.microsoft.com/download/4/C/4/4C402E48-0 ...
- iview input实现默认获取焦点并选中文字
1. 业务背景 配置页面,可新建和复制任务:当复制任务的时候,要把名字的input框默认获取焦点,并全选任务名.效果如下: 2. 代码实现 <template> <Form :mod ...
- Python基础教程(021)--Pycharm简介
前言 学习Pycharm开发工具 内容 项目:就是一个功能复杂的软件 目标 必须掌握的工具
- 黄金含量版本——KTV
呀,进来的都是盆友,首先先给大家拜年了,祝大家新年快乐,万事如意,家和万事兴~! 大家看了标题进来就不能让大家白进来,一定会让大家带着满满的果实. 下面我们就来讨论讨论KTV这个项目: (1)KTV的 ...
- Ant Design Pro (中后台系统)教程
一.概念:https://pro.ant.design/docs/getting-started-cn(官方网站) 1.Ant Design Pro 是什么: https://www.cnblogs ...
- (转)Java 原子性引用 AtomicReference
链接:https://www.jianshu.com/p/882d0e2c3ea6 來源:简书 作者:专职跑龙套 AtomicReference An object reference that m ...