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模式简单实现的更多相关文章

  1. 多线程系列之四:Guarded Suspension 模式

    一,什么是Guarded Suspension模式如果执行现在的处理会造成问题,就让执行处理的线程等待.这种模式通过让线程等待来保证实例的安全性 二,实现一个简单的线程间通信的例子 一个线程(Clie ...

  2. 并发设计模式之Guarded Suspension模式

    - 原文链接: http://www.joyhwong.com/2016/11/19/并发设计模式之guarded-suspension模式/ Guarded Suspension意为保护暂停,其核心 ...

  3. 并行模式之Guarded Suspension模式

    并行模式之Guarded Suspension模式 一).Guarded Suspension: 保护暂存模式 应用场景:当多个客户进程去请求服务进程时,客户进程的请求速度比服务进程处里请求的速度快, ...

  4. 多线程程序设计学习(4)guarded suspension模式

    Guarded Suspension[生产消费者模式] 一:guarded suspension的参与者--->guardedObject(被防卫)参与者                1.1该 ...

  5. 多线程同步循环打印和Guarded suspension 模式

     * 迅雷笔试题: * 有三个线程ID分别是A.B.C,请有多线编程实现,在屏幕上循环打印10次ABCABC…  由于线程执行的不确定性,要保证这样有序的输出,必须控制好多线程的同步. 线程同步有两种 ...

  6. 多线程学习之三生产者消费者模式Guarded Suspension

    Guarded Suspension[生产消费者模式] 一:guarded suspension的参与者--->guardedObject(被防卫)参与者                1.1该 ...

  7. Guarded Suspension Pattern【其他模式】

    Guarded Suspension Pattern public class GuardedSuspension { /** * Guarded Suspension Pattern[保护悬挂模式] ...

  8. Java设计模式之-----工厂模式(简单工厂,抽象工厂)

    一.工厂模式主要是为创建对象提供过渡接口,以便将创建对象的具体过程屏蔽隔离起来,达到提高灵活性的目的. 工厂模式在<Java与模式>中分为三类:1)简单工厂模式(Simple Factor ...

  9. 分布式系统的消息&服务模式简单总结

    分布式系统的消息&服务模式简单总结 在一个分布式系统中,有各种消息的处理,有各种服务模式,有同步异步,有高并发问题甚至应对高并发问题的Actor编程模型,本文尝试对这些问题做一个简单思考和总结 ...

随机推荐

  1. PL/SQL-FOR UPDATE 与 FOR UPDATE OF的区别

    PL/SQL-FOR UPDATE 与 FOR UPDATE OF的区别 数据库 oracle for update of   和   for update区别     select * from T ...

  2. 初识java-1.Java跨平台的原理

    计算机只认识二进制的机器指令,而且不同的平台的计算机的机器指令不同.Java中将程序员编写的源码文件编译成字节码文件,在不同的计算机平台上安装不同的虚拟机,虚拟机将同一份字节码文件解释为不同的机器指令 ...

  3. mybatis源码分析之03SqlSession的创建

    在上一篇中,说到了mybatis是如何构造一个SqlSessionFactory实例的,顾名思意,SqlSessionFactory就是用于创建SqlSession的工厂类. 好,现在我们接着昨天的来 ...

  4. Spring框架之接口实现覆盖(接口功能扩展)

    在日常开发中,存在着这种一种场景,框架对接口A提供了一个种默认的实现AImpl,随着需求的变更,现今AImpl不能满足了功能需要,这时,我们该怎么办? 当然是修改AImpl的实现代码了,但是,如果它是 ...

  5. spring-boot整合Mybatis多数据源案例

    1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 2.GITHUB地址 https://github.com/nbfujx/springBo ...

  6. Codeforces 814C - An impassioned circulation of affection

    原题链接:http://codeforces.com/contest/814/problem/C 题意:有长度为n的一个字符串,q个询问,每个询问由数字m和字符c组成,问最多在字符串中替换m个字符,使 ...

  7. ARC093 F Dark Horse——容斥

    题目:https://atcoder.jp/contests/arc093/tasks/arc093_d #include<cstdio> #include<cstring> ...

  8. JS基础入门篇(四)—this的使用,模拟单选框,选项卡和复选框

    1.this的使用 this js中的关键字 js内部已经定义好了,可以不声明 直接使用 this的指向问题 1. 在函数外部使用 this指向的是window 2. 在函数内部使用 有名函数 直接调 ...

  9. python中的_ElementUnicodeResult是什么

    _ElementUnicodeResult在python中是字符串的一种,因为在python3中,字符串就是指以unicode编码规则存储的数据,而以其他方式如utf-8,ASCII编码方式存储的数据 ...

  10. Codeforces Round #506 (Div. 3) E

    Codeforces Round #506 (Div. 3) E dfs+贪心 #include<bits/stdc++.h> using namespace std; typedef l ...