Guarded Suspension Pattern:保护暂停模式
设计方法
1。施加条件加以防卫
2。区分等待和不等待的情况
3。执行wait等待条件的变化
参与者
GuardedObject:被防卫的对象,有2个方法
guardedMethod方法
满足警戒条件时,马上执行
不满足警戒条件时,等待
stateChangingMethod方法
更改警戒条件
import java.util.Random;
public class ClientThread extends Thread {
private Random random;
private RequestQueue requestQueue;
public ClientThread(RequestQueue requestQueue, String name, long seed) {
super(name);
this.requestQueue = requestQueue;
this.random = new Random(seed);
}
public void run() {
for (int i = 0; i < 10000; i++) {
Request request = new Request("No." + i);
System.out.println(Thread.currentThread().getName() + " requests " + request);
requestQueue.putRequest(request);
try {
Thread.sleep(random.nextInt(1000));
} catch (InterruptedException e) {
}
}
}
}
public class Main {
public static void main(String[] args) {
RequestQueue requestQueue = new RequestQueue();
new ClientThread(requestQueue, "Alice", 3141592L).start();
new ServerThread(requestQueue, "Bobby", 6535897L).start();
}
}
public class Request {
private final String name;
public Request(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String toString() {
return "[ Request " + name + " ]";
}
}
import java.util.LinkedList;
public class RequestQueue {
private final LinkedList queue = new LinkedList();
public synchronized Request getRequest() {
while (queue.size() <= 0) {
try {
wait();
} catch (InterruptedException e) {
}
}
return (Request)queue.removeFirst();
}
public synchronized void putRequest(Request request) {
queue.addLast(request);
notifyAll();
}
}
import java.util.Random;
public class ServerThread extends Thread {
private Random random;
private RequestQueue requestQueue;
public ServerThread(RequestQueue requestQueue, String name, long seed) {
super(name);
this.requestQueue = requestQueue;
this.random = new Random(seed);
}
public void run() {
for (int i = 0; i < 10000; i++) {
Request request = requestQueue.getRequest();
System.out.println(Thread.currentThread().getName() + " handles " + request);
try {
Thread.sleep(random.nextInt(1000));
} catch (InterruptedException e) {
}
}
}
}
1. 将while改成if条件的问题
2.synchronized的范围只有wait
3.try..catch放在while 的外面
4.以Thread.sleep代替wait
分享到:
相关推荐
(注意,本资源附带书中源代码可供参考) 多线程与并发处理是程序设计好坏优劣的重要课题,本书通过浅显易懂的文字与实例来介绍Java线程相关的设计模式概念,并且通过实际的Java程序范例和 UML图示来一一解说,书中...
目录: 漫谈UML Introduction 1 Java语言的线程 Introduction 2 多线程...总结 多线程程序设计的模式语言 附录A 练习问题的解答 附录B Java的内存模型 附录C Java线程的优先级 附录D 线程相关的主要API 附录E 参考文献
Java多线程模式详解 目录: 一、漫谈UML Java语言的线程 多线程的评量标准 二、 1、Single Threaded Execution ———— 能通过这座桥的,只有一个人 2、Immutable ———— 想破坏它也没办法 3、Guarded ...
Guarded Suspension 模式 Producer Consumer 模式 Worker Thread 模式 Thread-Per-Message 模式 Future 模式 Read-Write-Lock 模式 Two-phase Termination 模式 Thread-Specific Storage 模式
│ 高并发编程第二阶段38讲、多线程Active Objects设计模式(接受异步消息的主动对象)-上.mp4 │ 高并发编程第二阶段39讲、多线程Active Objects设计模式(接受异步消息的主动对象)-中.mp4 │ 高并发编程第二阶段40...
│ 高并发编程第二阶段38讲、多线程Active Objects设计模式(接受异步消息的主动对象)-上.mp4 │ 高并发编程第二阶段39讲、多线程Active Objects设计模式(接受异步消息的主动对象)-中.mp4 │ 高并发编程第二阶段40...
今天小编就为大家分享一篇laravel中的fillable和guarded属性详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
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 ...
应该将其用于防止XSS攻击之类的东西,而不是用于隐藏敏感信息。 import guardedString from 'guarded-string' ; const myString = guardedString `My very important (but not too important) string` ; ...
JAX-RS 模板应用程序这是使用 JAX-RS 的轻量级 RESTful API 的模板。...在本地运行应用程序首先构建: $mvn clean install然后运行它: $ java -cp target/classes:target/dependency/* com.example.Main
scalac-guardedblocks-plugin 简单的Scala编译器插件
] 可以串联多个语句,并且最后一个语句后不能跟分号。 依次执行两个语句分配(:=) [; ] := <expression>[; <expression>] 将左侧的变量替换为右侧的值在右侧,不仅可以使用简单的数字,而且可以使用...
8.5 Guarded Commands 367 8.6 Conclusions 371 Summary • Review Questions • Problem Set •Programming Exercises 372 Chapter 9 Subprograms 377 9.1 Introduction 378 9.2 Fundamentals of ...
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 ...
防止在异步操作正在进行时多次调用异步函数。 操作完成后,将进行操作进行时对该函数的最后一次调用。 中间调用将丢失,如果在异步操作正在进行时没有进行调用,则不会再次调用该函数。 这种去抖动不是基于时间,...
我们遵循使用Node和MySQL的MVC设计模式来查询和路由应用程序中的数据。 把手用于生成HTML。 Travis CI用于配置棉绒并进行连续的分割。然后将该项目加载到heroku中,以与JawsDB MySQL附加组件一起用作数据库。目录...
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...
消息处理机制主要对象:Looper,Handler,Message(还有...Looper.java static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal(); private static Looper sMainLooper; // guarded by Looper.class final
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...