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();    }}
原创粉丝点击