Java多线程 Guarded Suspension设计模式
作者:冬日毛毛雨 发布时间:2023-03-12 01:52:00
前言:
Guarded Suspension
意为保护暂停,其核心思想是仅当服务进程准备好时,才提供服务。设想一种场景,服务器可能会在很短时间内承受大量的客户端请求,客户端请求的数量可能超过服务器本身的即时处理能力,而服务端程序又不能丢弃任何一个客户请求。此时,最佳的处理方案莫过于让客户端要求进行排队,由服务端程序一个接一个处理。这样,既保证了所有的客户端请求均不丢失,同时也避免了服务器由于同时处理太多的请求而崩溃
1.Guarded Suspension模式的结构
Guarded Suspension模式的主要成员有:Request
、RequestQueue
、ClientThread
、 ServerThread
Request
:表示客户端请求RequestQueue
:用于保存客户端请求队列ClientThread
:客户端进程ServerThread
:服务器进程
其中,ClientThread
负责不断发起请求,并将请求对象放入请求队列。ServerThread
则根据其自身的状态,在有能力处理请求时,从RequestQueue
中提取请求对象加以处理。
从流程图中可以看到,客户端的请求数量超过了服务线程的能力。在频繁的客户端请求中,RequestQueue
充当了中间缓存,存放未处理的请求,保证了客户请求不丢失,同时也保护了服务线程不会受到大量并发的请求,而导致计算机资源不足
2. Guarded Suspension模式的简单实现
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.
来源:https://juejin.cn/post/7022609159912685575


猜你喜欢
- 前言在最近的一个项目中做了一个涂鸦的效果,手指快速移动,会出现折线,这篇文章记录笔触优化。下面话不多说了,来一起看看详细的介绍吧。优化前优化
- 普通的公有继承class test1{public: test1(int i) :num1(i) {}privat
- 本文实例讲述了Android TextView实现带链接文字事件监听的三种常用方式。分享给大家供大家参考,具体如下:/** * TextVi
- Activity是Android组件中最基本也是最为常见用的四大组件之一,在 android开发中 ,运用极为广泛,作为初学者需要熟练掌握,
- 因为线程重用导致的信息错乱的bugThreadLocal一般用于线程间的数据隔离,通过将数据缓存在ThreadLocal中,可以极大的提升性
- 引言相信伙伴们在日常的开发工作中,一定会遇到事件冲突的问题,e.g. 一个页面当手指滑动的时候,会翻到下一页;点击的时候,需要响应页面中的元
- 我们在编写Web应用时,经常需要对页面做一些安全控制,比如:对于没有访问权限的用户需要转到登录表单页面。要实现访问控制的方法多种多样,可以通
- 本文实例为大家分享了java实现购物车功能的具体代码,供大家参考,具体内容如下1 需要实现1、实现淘淘商城的购物车功能2 购物车功能2.1
- 本文为大家分享了Android实现水波纹效果展示的具体代码,供大家参考,具体内容如下一、效果二、实现原理自定义view,使用Path和贝塞尔
- Android自带的SeekBar是水平的,要垂直的,必须自己写一个类,继承SeekBar。一个简单的垂直SeekBar的例子:(但是它其实
- 普通校验导入依赖:默认的报错:没有提示具体的属性设置自己的错误信息提示:创建 ValidationMessages.properties内容
- 现在的应用在注册登录或者修改密码中都用到了短信验证码,那在android中是如何实现获取短信验证码并自动填写的呢?首先,需要要在manife
- 前言泛型在java中有很重要的地位,无论是开源框架还是JDK源码都能看到它。毫不夸张的说,泛型是通用设计上必不可少的元素,所以真正理解与正确
- 最近遇到了一个问题,一份很老的代码要修改里面的变量,源码早就和开发者一起不知去向,其中引用了一些jar包导致无法直接编译,只能直接修改.cl
- 准备三个框架结合的lib包Spring3结合Struts2的步骤如下:1:开启Struts2结合Spring3,在struts.xml中添加
- 本文实例讲述了Java中的多态用法。分享给大家供大家参考。具体分析如下:多态,是面向对象的程序设计语言最核心的特征。封装性、继承性都比较简单
- 一、概述1、XMLReader为抽象类,其派生类有:XmlDictionaryReaderXmlNodeReaderXmlTextReade
- Java中有两类线程:User Thread(用户线程)、Daemon Thread(守护线程)用户线程即运行在前台的线程,而守护线程是运行
- 本文实例讲述了Android简单获取经纬度的方法。分享给大家供大家参考,具体如下:public void getLoc() {
- 这篇文章主要介绍了Spring Boot Logback配置日志过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考