java web week_five 多线程作业
2017-06-06 12:57
344 查看
作业要求:
模拟一个储蓄卡账户,起初额度是100元,模拟取现、存款等多次操作。取现及存款分别使用不同的线程,注意线程同步问题。
cangku:
CostMoney:
StoreMoney:
Test:
模拟一个储蓄卡账户,起初额度是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(); } }
相关文章推荐
- 【Java作业】Week10——多线程
- java Web技术作业二
- java多线程模拟有会话的访问web系统,并记录访问日志
- 2016 java web 期末大作业心得体会 -- 小型的新闻发布系统(承认自己做的很烂)
- JSP+Servlet培训班作业管理系统[23] -番外篇之Java Web日志
- JAVA 编写一个多线程的简单Web服务器
- JavaWeb之Httpt(1)------>作业
- java web week two
- IT十八掌作业_java基础第九天_多线程、自动拆装箱
- 作业_java基础第八天_多线程
- 知乎问题:北京,2017,多少k的java web程序员应该懂多线程和jvm优化?
- java+oracle+web(第九天) java 基础课程(四) 多线程其他小知识 终止、守护、等待
- IT十八掌作业_java基础第九天_多线程、自动拆装箱
- IT十八掌作业_java基础第八天_多线程
- 作业_java基础第九天_多线程、自动拆装箱
- Web 多线程作业
- 我的Java Web作业---个人简历
- IT十八掌作业_java基础第九天_多线程、自动拆装箱
- 多线程Web服务器的设计与实现(JAVA与PYTHON)
- java web week_one