Java 第十次作业

(一)学习总结###

1.思维导图###

2.用实现Runnable接口的方式、多线程实现龟兔赛跑小游戏###

  • 补充Tortoise线程类
class Tortoise implements Runnable {
    private int total;
    private int mytotal = 0;
    private boolean game =true;
    public Tortoise(int total) {
        this.total = total;
    }

    public void run() {
        while (game) {
            if (mytotal < total) {
                try {
                    Thread.sleep(800);
                    int step = (int)(Math.random()*5+1);
                    mytotal+=step;
                    System.out.println("小乌龟走"+mytotal+"步");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                System.out.println("小乌龟到达终点");
                game=false;
            }
        }
    }
}

  • 补充Hare线程类
class Hare implements Runnable {
    private int total;
    private int mytotal = 0;
    private boolean game =true;
    public Hare(int total) {
        this.total = total;
    }

    public void run() {
        while (game) {
            if (mytotal < total) {
                try {
                    Thread.sleep(800);
                    int step = (int)(Math.random()*5+1);
                    int flag = (int)(Math.random()*2+1);
                    if(flag==1){
                        System.out.println("小兔子累啦,小兔子要睡觉");
                    }else{
                        mytotal+=step;
                        System.out.println("小兔子走"+mytotal+"步");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                System.out.println("兔子到达终点");
                game=false;
            }
        }
    }
}

3.生产者——消费者问题的程序分析###

  • 修改的部分的程序
public void run() {
        System.out.println("消费者开始消耗整数......");
        // 消耗10个整数
        for (int i = 1; i <= 10; i++) {
            try {
                // 等待随机时间
                Thread.sleep((int) (Math.random() * 3000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
class Clerk {
    private int product = -1; // -1 表示目前没有产品
    private int p;
    // 这个方法由生产者呼叫
    public synchronized void setProduct(int product) {
        if (this.product != -1) {
            try {
                super.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.product = product;
        p = this.product;
        System.out.printf("生产者设定 (%d)%n", this.product);
        getProduct();
        this.product = -1;
        super.notify();
    }

    // 这个方法由消费者呼叫
    public synchronized int getProduct() {
        if (this.product == -1) {
            try {
                super.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.printf("消费者取走 (%d)%n", p);
        this.product = -1;
        super.notify();
        return this.product;
    }
}

4.其他需要总结的内容###

  • 应用Thread类的子类来实现

  • 应用Runnale接口来实现

(二)实验总结###

1.分发作业###

  • 程序设计思路###

    • 创建三个线程,每个老师相当于一个线程

    • 用Runnable接口,实现run()方法

    • 用Thread类的start()方法启动线程

  • 程序运行结果###


2.银行存款###

  • 程序设计思路###

    • 设计一个银行类,定义一个存储账户余额的变量以及一个存款的方法

    • 设计一个储户类,实现向账户存款3次,每一个储户是一个线程

    • 设计一个测试类,创建两个客户对象,两个客户就是两个线程,启动线程。

  • 程序运行结果###

  • 问题1###

    • 程序运行停不下来
  • 原因###

    • 不是saveMoney而是saveMOney--
  • 问题2###

    • 不知怎么实现,存款之后,怎么立即更新账户余额
  • 解决方案###

    • 银行类中添加存款方法setMoney(int money)方法 ,不是写this.money += money;而是写this.money += money;

(三)代码托管###

代码链接###

https://git.oschina.net/hebau_cs15/hebau-cs01GHJ.git

码云截图###

commit历史截图###

posted @ 2017-06-02 21:47  长长长长的街道  阅读(374)  评论(0编辑  收藏  举报