`

Guarded Suspension Pattern

阅读更多
防卫暂停模式
Guarded Suspension Pattern的参与者:(对应Java设计模式中的"生产消费者模式")
GuardedObject(被防卫的对象)参与者,GuardedObject参与者是一个拥有被防卫的方法(guardedMethod)的类,当线程执行guardedMethod时,只要满足警戒条件,就会马上执行,但警戒条件不成立时,就要开始等待,警戒条件的成立与否,会随GuardedObject参与者的状态改变而变化;GuardedObject参与者除了guardedMethod以外,可能还会有用来更改实例状态(特别是用来更改警戒条件)的方法(stateChangingMethod);Java语言中,使用while语句与wait方法来实现guardedMethod的,而是用notify/notifyAll方法来实现stateChangingMethod,在以下范例程序中,RequestQueue类就是GuardedObject参与者,getRequest方法与putRequest方法则分别是guardedMethod与stateChangingMethod
========Request类
public class Request {

private final String name;

public Request(String name) {
this.name = name;
}

public final String getName() {
return name;
}

public String toString() {
return "[ Request " + this.name + " ]";
}
}

=======RequestQueue类
public class RequestQueue {

private final LinkedList<Request> queue = new LinkedList();

public synchronized Request getRequest() {
try {
while (0 >= queue.size()) {
this.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return (Request) queue.removeFirst();
}

public synchronized void putRequest(Request request) {
queue.addLast(request);
notifyAll();
}
}
=======ClientRequest类
public class ClientRequest extends Thread {

private Random random;
private RequestQueue queue;

public ClientRequest(RequestQueue queue, String name, long seed) {
super(name);
this.random = new Random(seed);
this.queue = queue;
}

public void run() {
try {
Request request = null;
for (int i = 0; i < 10000; i++) {
request = new Request("No." + i);
System.out.println(Thread.currentThread().getName()
+ " requests " + request);
queue.putRequest(request);
Thread.sleep(this.random.nextInt(1000));
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
=======ServerReqiest类
public class ServerRequest extends Thread {

private Random random;
private RequestQueue queue;

public ServerRequest(RequestQueue queue, String name, long seed) {
super(name);
this.queue = queue;
this.random = new Random(seed);
}

public void run() {
try {
Request request = null;
for (int i = 0; i < 10000; i++) {
request = queue.getRequest();
System.out.println(Thread.currentThread().getName()
+ " handles " + request);
Thread.sleep(this.random.nextInt(1000));
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
=========测试类
public class Main {

public static void main(String[] args) {
RequestQueue queue = new RequestQueue();
new ClientRequest(queue, "Alice", 3141592L).start();
new ServerRequest(queue, "Bobby", 6535897L).start();
}
}
分享到:
评论

相关推荐

    36种最新设计模式整理

    Guarded Suspension 模式 Producer Consumer 模式 Worker Thread 模式 Thread-Per-Message 模式 Future 模式 Read-Write-Lock 模式 Two-phase Termination 模式 Thread-Specific Storage 模式

    Java多线程详解

    3、Guarded Suspension ———— 要等到我们准本好哦 4、Balking ———— 不需要的话,就算了吧 5、Producer-Consumer ———— 我来做,你来用 6、Read-Write Lock ———— 大家想看就看吧,不过看的时候不能写哦...

    java多线程设计模式 (PDF中文版, 附源码)

    第3章 Guarded Suspension——要等到我准备好喔 第4章 Balking——不需要的话,就算了吧 第5章 Producer-Consumer——我来做,你来用 第6章 Read-Write Lock——大家想看就看吧,不过看的时候不能写喔 第7章 read-...

    laravel中的fillable和guarded属性详解

    今天小编就为大家分享一篇laravel中的fillable和guarded属性详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

    guarded-array:边界检查数组

    var guard = require ( 'guarded-array' ) //First create any old array var array = [ 0 , 1 , 2 , 3 , 4 , 5 ] //Then we protect it using guard! var guardedArray = guard ( array ) //The guarded array ...

    guarded-string:防止在应用程序中意外地在字符串中引入XSSKong

    yarn add guarded-string 用法 重要的! 应该将其用于防止XSS攻击之类的东西,而不是用于隐藏敏感信息。 import guardedString from 'guarded-string' ; const myString = guardedString `My very important (but ...

    java多线程设计模式详解(PDF及源码)

    第1章 Single Threaded Execution——能通过这座桥的,只有一个人 第2章 Immutable——想破坏它也没办法 第3章 Guarded Suspension——要等到我准备好喔 第4章 Balking——不需要的话,就算了吧 第5章 Producer-...

    scalac-guardedblocks-plugin:简单的Scala编译器插件

    scalac-guardedblocks-plugin 简单的Scala编译器插件

    guarded-bayou-7383

    JAX-RS 模板应用程序这是使用 JAX-RS 的轻量级 RESTful API 的模板。 示例代码是获取当前时间的调用。在本地运行应用程序首先构建: $mvn clean install然后运行它: $ java -cp target/classes:target/dependency/*...

    GuardedCommands:用于模拟防护命令语言的Python程序

    防护命令语言六月7,2018 CS522正式语言和自动机20183111金李涵概括 2018年Spring正式语言和自动机理论项目1〜2 防护命令语言(以下称为GCL)解析和执行功能的实现语句语法说明中止论文说“做任何事情”,但实际上并...

    汪文君高并发编程实战视频资源全集

    │ 高并发编程第二阶段25讲、Guarded Suspension设计模式-下.mp4 │ 高并发编程第二阶段26讲、ThreadLocal使用详解,深入原理介绍.mp4 │ 高并发编程第二阶段27讲、多线程运行上下文设计模式介绍.mp4 │ 高并发...

    汪文君高并发编程实战视频资源下载.txt

    │ 高并发编程第二阶段25讲、Guarded Suspension设计模式-下.mp4 │ 高并发编程第二阶段26讲、ThreadLocal使用详解,深入原理介绍.mp4 │ 高并发编程第二阶段27讲、多线程运行上下文设计模式介绍.mp4 │ 高并发...

    butterknife 8.8.0

    When specified as false, checks for required views being non-null are elided and casts are no longer guarded with user-friendly error messages. This reduces the amount of generated code for release ...

    AsyncDebounce:防止在异步操作正在进行时多次调用异步函数

    异步去抖 防止在异步操作正在进行... var guarded = debounce(function (args, callback) { // do something with the arguments, then call the callback }); guarded('hello', 'world'); // Parameters are comb

    Understanding_Ipv6

    These details are highly guarded Microsoft intellectual property that is of interest only to a relative handful of software developers. The purpose of this book is to provide an educational vehicle...

    understanding IPv6

    These details are highly guarded Microsoft intellectual property that is of interest only to a relative handful of software developers. The purpose of this book is to provide an educational vehicle...

    Laravel-Overflow:Laravel Overflow包将允许轻松地向表单请求中添加一个溢出列。 使用此程序包可以轻松地将溢出请求值存储在数据库表的JSON或Text列中

    Laravel溢出 Laravel Overflow包将允许轻松地向表单请求中添加一个溢出列。 使用此程序包可以轻松地将溢出请求值存储在数据库表的JSON或Text列中:) 安装 您可以通过composer安装该软件包: ... protected $ guarded

    heroku-destroy-temp:Heroku CLI插件可销毁临时应用

    3. guarded-journey-99652 4. floating-atoll-86710 5. obscure-anchorage-63377 Destroy these 5 apps? (y/n): y destroying apps... done 安装 $ heroku plugins:install heroku-destroy-temp

    model:该模型提供了一个雄辩的基类,可用于在Laravel和其他框架中构建自定义模型

    模型 该模型提供了类似Laravel雄辩的基类,可用于在Laravel或其他框架中构建自定义模型。特征存取器和更改器模型到数组和JSON转换数组... protected $ guarded = [ 'password' ]; protected $ casts = [ 'age' =&gt; 'inte

Global site tag (gtag.js) - Google Analytics