博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Exchanger 应用
阅读量:6857 次
发布时间:2019-06-26

本文共 2626 字,大约阅读时间需要 8 分钟。

hot3.png

import java.util.concurrent.Exchanger;/** * 案例:服务员向原有空杯中不断倒水,消费者不断从原有装满水的杯中喝水。  * 当服务员倒满水和消费者喝完水时,两个杯子进行交换。一直这样周而复始。 *  * @author Administrator *  */public class TestExchanger {	static Exchanger
exchanger = new Exchanger
(); static Cup emptyCup = new Cup(0); static Cup fullCup = new Cup(100); public static void main(String[] args) { new Thread(new Waiter(3)).start(); new Thread(new Customer(6)).start(); } /** * 服务员 * * @author Administrator * */ static class Waiter implements Runnable { int addSpeed = 1; public Waiter(int addSpeed) { this.addSpeed = addSpeed; } @Override public void run() { while (emptyCup != null) { // 如果发现杯子中的水没有满,则往杯中加水。 try { if (emptyCup.isFull()) {// 当emptyCup为装满水时并且fullCup为被喝完水时,进行交换。 // 当Waiter把杯子水装满后,与Customer交换杯子。 // exchange(emptyCup)中参数的emptyCup是waiter装满水后的,把装满水后的emptyCup参数传递给Cutomer线程。 // 并且返回Cutomer线程中fullCup给Waiter的emptyCup,这样就达到了交换效果。 emptyCup = exchanger.exchange(emptyCup); System.out.println("Waiter: " + emptyCup.capacity); } else { emptyCup.addWaterToCup(3); System.out.println("Waiter add " + addSpeed + " and current capacity is " + emptyCup.capacity); Thread.sleep((long) (Math .max(500, Math.random() * 2000))); } } catch (InterruptedException e) { e.printStackTrace(); } } } } /** * 消费者 * * @author Administrator * */ static class Customer implements Runnable { int drinkSpeed = 1; public Customer(int drinkSpeed) { this.drinkSpeed = drinkSpeed; } @Override public void run() { while (fullCup != null) { // 如果发现杯子中的水没有满,则往杯中加水。 try { if (fullCup.isEmpty()) { // 水满后则交换子中的水 fullCup = exchanger.exchange(fullCup); System.out.println("Customer: " + fullCup.capacity); } else { fullCup.drinkFormCup(drinkSpeed); System.out.println("Customer drinked " + drinkSpeed + " and current capacity is " + fullCup.capacity); Thread.sleep((long) (Math .max(500, Math.random() * 2000))); } } catch (InterruptedException e) { e.printStackTrace(); } } } } static class Cup { private int capacity = 0; public Cup(int capacity) { this.capacity = capacity; } public void addWaterToCup(int i) { capacity += i; capacity = capacity > 100 ? 100 : capacity; } public void drinkFormCup(int i) { capacity -= i; capacity = capacity < 0 ? 0 : capacity; } public boolean isFull() { return capacity == 100 ? true : false; } public boolean isEmpty() { return capacity == 0 ? true : false; } }}

转载于:https://my.oschina.net/u/138995/blog/106589

你可能感兴趣的文章
java基础(二)
查看>>
cocos2d中的anchorPoint
查看>>
记录一下:chrome上,把网页保存为文件的插件
查看>>
C#和Javascript间互转的Xxtea加解密
查看>>
BAT批处理中的字符串处理详解(字符串截取)
查看>>
智力题集锦【二】
查看>>
读 《我为什么放弃Go语言》 有感
查看>>
删除MySQL中冗余字段
查看>>
linux基础—课堂随笔_03 SHELL脚本编程基础
查看>>
【Win7快捷键启动程序有哪些妙招】
查看>>
MS DOS 命令大全
查看>>
College student reflects on getting started in open source(一)
查看>>
Windows下初次手动安装composer详细教学
查看>>
Oracle 查询库中所有表名、字段名、字段名说明,查询表的数据条数、表名、中文表名、...
查看>>
JAVA入门到精通-第53讲-数据库概念
查看>>
升级10.10 Yosemite 后,cocoapods 出现错误(解决方案)
查看>>
[Microsoft][ODBC 驱动程序管理器] 在指定的 DSN 中,驱动程序和应用程序之间的体系结构不匹配...
查看>>
SQL ROW_NUMBER() 分页使用示例
查看>>
UEditor编辑器两个版本任意文件上传漏洞分析
查看>>
Redis分布式锁服务(八)
查看>>