Guarded Suspension 设计模式可以保证,当线程在访问某个对象时,发现条件不满足,就挂起等待条件满足时再次访问
public class GuardedSuspensionQueue {

    // 定义存放Integer类型的queue
private final LinkedList<String> queue = new LinkedList<String>(); // 定义queue的最大容量为5
private final int LIMIT = ; public static final Random random = new Random(System.currentTimeMillis()); // 往queue中插入数据,如果queue中的元素超过了最大容量,则会陷入阻塞
public void offer(String data) {
synchronized (this) {
// 判断queue的当前元素是否超过了LIMIT
while (queue.size() >= LIMIT) {
// 挂起当前线程,使其陷入阻塞
System.out.println("向queue中添加元素,元素个数超过了限制,"+Thread.currentThread().getName()+"被挂起!");
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 插入元素并且唤醒take线程
queue.addLast(data);
this.notifyAll();
}
} // 从队列中获取元素,如果队列此时为空,则会使当前线程阻塞
public String take() {
synchronized (this) {
// 判断如果队列为空
while (queue.isEmpty()) {
// 则挂起当前线程
System.out.println(Thread.currentThread().getName()+"从queue中取出元素,元素个数为0,被挂起!");
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
//removeFirst不会返回null值,queue没有元素会抛出异常
String res = queue.removeFirst();
// 通知offer线程可以继续插入数据了
this.notifyAll();
return res;
}
} public static void main(String[] args) throws InterruptedException { GuardedSuspensionQueue queue = new GuardedSuspensionQueue(); TakerThread takerThread = new TakerThread(queue,"【线程A】");
takerThread.start(); Thread.sleep(); // 创建3个生产线程向queue中添加数据
IntStream.rangeClosed(, ).forEach(i -> {
new Thread(() -> {
for (int j = ; j <= ; j++) {
queue.offer("【线程"+i+"】放入的"+j);
}
try {
Thread.sleep(random.nextInt());
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}); Thread.sleep(10_000); takerThread.close();
} } class TakerThread extends Thread{
private volatile boolean close = false;
private GuardedSuspensionQueue queue ;
TakerThread(GuardedSuspensionQueue queue,String name){
this.queue = queue;
setName(name);
}
@Override
public void run() {
while(!close) {
String item = queue.take();
if(item == null) {
break;
}
System.out.println(Thread.currentThread().getName() + "从queue中取出数据:" + item);
try {
Thread.sleep(GuardedSuspensionQueue.random.nextInt());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
break;
}
}
} public void close() {
this.close = close;
this.interrupt();
} }
 

Guarded Suspension设计模式的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. 并行模式之Guarded Suspension模式

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

  7. Guarded Suspension Pattern【其他模式】

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

  8. Guarded Suspension模式简单实现

    Guarded Suspension 意为保护暂停,假设服务器很短时间内承受大量的客户端请求,客户端请求的数量超过服务器本身的即时处理能力,而服务器又不能丢弃任何一个客户端请求,此时可以让客户端的请求 ...

  9. 多线程编程-设计模式之保护性暂挂(Guarded Suspesion)模式

    Guarded Suspension模式的架构 核心是一个受保护方法(Guarded Method).该方法需要执行其所要真正执行的操作时需要满足特定的条件(Predicate,以下称之为保护条件). ...

随机推荐

  1. 4. 移动安全渗透测试-(Android逆向基础)

    4.1 smali 基础 1.注释 smali中使用#来代表注释一行例如:# const-string v0, "aaa" #这句不会被执行 2.数据类型 V void,只能用于返 ...

  2. flink WaterMark之TumblingEventWindow

    1.WaterMark,翻译成水印或水位线,水印翻译更抽象,水位线翻译接地气. watermark是用于处理乱序事件的,通常用watermark机制结合window来实现. 流处理从事件产生,到流经s ...

  3. SQL的简单函数(极客时间)

    SQL的函数 函数就是将我们经常使用的代码封装起来, 需要的时候直接调用, 提高代码效率和可维护性. SQL函数一般是在数据上执行, 可以方便地转换和处理数据. 常用的SQL函数 SQL提供了一些常用 ...

  4. Python Scrapy在windows上的安装方法

    如果想要学习爬虫,肯定会了解Scrapy,但安装Scrapy的方法相对于安装其他第三方库麻烦一点. 下面总结一下在我的电脑上安装Scrapy的方法,我的电脑是Windows10,32位操作系统.有如下 ...

  5. win7 安装vb6

    1. 用setup.exe有问题,用acmsetup.exe 2.打开setupwiz.ini,把"acme=acmboot.exe"改为"=setup\acmsetup ...

  6. odoo10学习笔记十二:web controller

    转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/11189332.html 一:路由 odoo.http.route(route=None, **kw) 装饰器 ...

  7. LCA最近公共祖先-- HDU 2586

    题目链接 Problem Description There are n houses in the village and some bidirectional roads connecting t ...

  8. IDEA 一键生成所有setter方法(GenerateAllSetter插件)

    GenerateAllSetter插件使用效果如下: alt+enter快捷键选择Generate all setter 之后就会自动生成其中的所有setter方法 下面介绍idea安装步骤: alt ...

  9. echo和printf打印输出

    [root@node2 scprits]# echo Hello World! Hello World! [root@node2 scprits]# echo 'Hello World!' Hello ...

  10. web-天下武功唯快不破

    没有武术是不可摧毁的,而最快的速度是获得长期成功的唯一途径.>>>>>> ----你必须尽可能快地做到这一点!---- <<<<<&l ...