java web week_five 多线程作业
来源:互联网 发布:软件项目水果商场 编辑:程序博客网 时间:2024/06/08 18:54
作业要求:
模拟一个储蓄卡账户,起初额度是100元,模拟取现、存款等多次操作。取现及存款分别使用不同的线程,注意线程同步问题。
cangku:
public class CangKu { private static final int max_size = 100; //最大库存量 private int curnum; private Lock lock = new ReentrantLock(); private Condition conditionProduce = lock.newCondition(); private Condition conditionConsume = lock.newCondition(); CangKu(int curnum) { this.curnum = curnum; } public void produce(int neednum) { lock.lock(); try { while (neednum + curnum > max_size) { System.out.println("要存入的money: " + neednum + " 超过剩余可存量 " + (max_size - curnum) + ",暂时不能执行存钱任务!"); conditionProduce.await(); } curnum += neednum; System.out.println("已经存了了 " + neednum + " money,现仓储量为: " + curnum); //唤醒在此对象监视器上等待的线程 conditionConsume.signal(); }catch (Exception e){ e.printStackTrace(); }finally { lock.unlock(); } } public void consume(int neednum) { lock.lock(); try { while (curnum < neednum) { conditionConsume.await(); } curnum -= neednum; System.out.println("已经花费了 " + neednum + " money,现仓储量为: " + curnum); //唤醒在此对象监视器上等待的线程 conditionProduce.signal(); }catch (Exception e){ e.printStackTrace(); }finally { lock.unlock(); } } public boolean status() throws InterruptedException { if (lock.tryLock()){ if (conditionProduce.await(1, TimeUnit.SECONDS) && conditionProduce.await(1, TimeUnit.SECONDS)){ return true; } } return false; }}
CostMoney:
public class CostMoney extends Thread { private int needNum; private CangKu cangKu; CostMoney(int neednum, CangKu cangKu) { this.needNum = neednum; this.cangKu = cangKu; } public void run() { cangKu.consume(needNum); }}
StoreMoney:
public class StoreMoney extends Thread { private int needNum; private CangKu cangKu; StoreMoney(int neednum, CangKu cangKu) { this.needNum = neednum; this.cangKu = cangKu; } public void run() { cangKu.produce(needNum); }}
Test:
public class Test { public static void main(String[] args) { CangKu godown = new CangKu(100); StoreMoney s1 = new StoreMoney(1, godown); StoreMoney s2 = new StoreMoney(1, godown); StoreMoney s3 = new StoreMoney(1, godown); StoreMoney s4 = new StoreMoney(1, godown); StoreMoney s5 = new StoreMoney(1, godown); StoreMoney s6 = new StoreMoney(1, godown); StoreMoney s7 = new StoreMoney(1, godown); CostMoney c1 = new CostMoney(1, godown); CostMoney c2 = new CostMoney(1, godown); CostMoney c3 = new CostMoney(1, godown); CostMoney c4 = new CostMoney(1, godown); CostMoney c5 = new CostMoney(1, godown); CostMoney c6 = new CostMoney(1, godown); CostMoney c7 = new CostMoney(1, godown); ExecutorService pool = Executors.newCachedThreadPool(); for (int i = 0; i < 100; i++) { pool.execute(s1); pool.execute(s2); pool.execute(s3); pool.execute(c1); pool.execute(c2); pool.execute(c3); pool.execute(c4); pool.execute(c5); pool.execute(s4); pool.execute(s5); pool.execute(s6); pool.execute(s7); pool.execute(c6); pool.execute(c7); System.out.println("i = " + i); } pool.shutdown(); }}
阅读全文
0 0
- java web week_five 多线程作业
- Web 多线程作业
- java多线程作业
- 第十五周作业:Java多线程
- 我的Java Web作业---计算器作业
- java web实验作业9
- java多线程web服务器
- Java作业-猜数字(多线程)
- 【Java作业】Week10——多线程
- 多线程作业
- 多线程作业
- 多线程作业
- 多线程作业
- 多线程作业
- 我的Java Web作业---百度音乐网站
- 我的Java Web作业---个人简历
- Web作业
- JAVA作业 多线程利用join方法计算随机数的最大值
- mybatisGenerator 代码自动生成报错 Result Maps collection already contains value for BaseResultMap
- 35_面向对象_08_方法重载_构造方法重载
- 推荐算法中的 相似度计算
- 使用python删除本地夹里重复的文件
- 将Centos的yum源更换为阿里云源
- java web week_five 多线程作业
- gcc 版本下载地址和编译依赖库
- STL案例之学校演讲比赛
- 基于Netty4的HttpServer和HttpClient的简单实现
- 输入任意两正整数,获取该区间所有素数
- Fragment的基本概述
- java对配置文件.properties文件读取的几种方法
- 干TM的gradle,让我又爱又恨(不过几乎都是小问题23333333)
- Redis-HelloWorld与五大基本数据类型(笔记)