Java第十次作业 1502 马 帅

《Java技术》第十次作业

(一)学习总结

1. 

2.如下代码:

public class TortoiseHareRace {
    public static void main(String[] args) {
        int totalStep = 10;
        int tortoiseStep = 0;
        int hareStep = 0;
        boolean[] flags = {true,false};
        System.out.println("龟兔赛跑开始了...");
        while(tortoiseStep < totalStep && hareStep < totalStep){
            tortoiseStep++;
            System.out.println("乌龟跑了"+tortoiseStep+"步...");
            boolean isHareSleep = flags[((int)(Math.random()*10))%2];
            if(isHareSleep){
                System.out.println("兔子睡着了zzzz");
            }else{
                hareStep += 2;
                System.out.println("兔子跑了"+hareStep+"步...");
            }
        }       
    }
}

使用Runnable接口完善后的代码如下:

public class TortoiseHareRace { 
    public static void main(String[] args) {
        Tortoise tortoise = new Tortoise(10);
        Hare hare = new Hare(10);
        Thread tortoiseThread = new Thread(tortoise);
        Thread hareThread = new Thread(hare);
        System.out.println("龟兔赛跑开始了...");
        tortoiseThread.start();
        hareThread.start();
    }
}
class Tortoise implements Runnable{
	private int step;
	public Tortoise(){}
	public Tortoise(int step){
		this.step=step;
	}
	public void run() {
        int tortoiseStep = 0;
        while(tortoiseStep < step){
            try {
                Thread.sleep(1000);
                tortoiseStep++;
                System.out.println("乌龟跑了"+tortoiseStep+"步...");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } 
    }
	
}
class Hare implements Runnable{
	private int step;
	public Hare(){}
	public Hare(int step){
		this.step=step;
	}
	public void run() {
        int hareStep = 0;
        boolean[] flags = {true,false};
        while(hareStep <step){
            try {
                Thread.sleep(1000);
                boolean isHareSleep = flags[((int)(Math.random()*10))%2];
                if(isHareSleep){
                    System.out.println("兔子睡着了zzzz");
                }else{
                    hareStep += 2;
                    System.out.println("兔子跑了"+hareStep+"步...");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }  
    }
	
}

3.如下代码:

class Consumer implements Runnable {
    private Clerk clerk;
    public Consumer(Clerk clerk) {
        this.clerk = clerk;
    }
    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();
            }              
            clerk.getProduct();// 从店员处取走整数
        }
    }
 }
class Producer implements Runnable {
    private Clerk clerk;
    public Producer(Clerk clerk) {
        this.clerk = clerk;
    }
    public void run() {
        System.out.println( "生产者开始生产整数......");
        // 生产1到10的整数
        for(int product = 1; product <= 10; product++) {
            try {
                Thread.sleep((int) Math.random() * 3000);
            }
            catch(InterruptedException e) {
                e.printStackTrace();
            }
           clerk.setProduct(product); // 将产品交给店员
        }
    } 
}
public class ProductTest {
    public static void main(String[] args) {
        Clerk clerk = new Clerk();
        Thread consumerThread = new Thread(new Consumer(clerk)); 
        Thread producerThread = new Thread(new Producer(clerk)); 
        consumerThread.start(); 
        producerThread.start(); 
    }
}
class Clerk {
    private int product = -1; // -1 表示目前没有产品 
     // 这个方法由生产者呼叫
    public void setProduct(int product) {
        this.product = product; 
        System.out.printf("生产者设定 (%d)%n", this.product);      
    } 
    // 这个方法由消费者呼叫
    public int getProduct() {          
        int p = this.product; 
        System.out.printf("消费者取走 (%d)%n", this.product);      
        return p; 
    } 
}

运行结果为

没有进行同步,而且当生产者生产过快时,消费者可能不会取走产品,修改Clerk类:

class Clerk {
    private int product = -1; // -1 表示目前没有产品 
     // 这个方法由生产者呼叫
    public synchronized void setProduct(int product) {        //同步代码
        this.product = product; 
        System.out.printf("生产者设定 (%d)%n", this.product);
        try {
			wait();                                //线程中断
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
    } 
    // 这个方法由消费者呼叫
    public synchronized int getProduct() {          
        int p = this.product; 
        System.out.printf("消费者取走 (%d)%n", this.product);
        notify();                                                //唤醒线程
        return p; 
    } 
}

(二)实验总结

1.模拟三个老师同时分发80分作业,每个老师相当于一个线程。

  • 程序设计思路:
    声明Teacher类继承Runnable接口,定义homework值为1,重写run()方法,使用循环,每次调用时输出当前线程和分发作业数。
    在main()中,声明一个Teacher类的对象,和三个Thread类的线程,作为三个老师,分别使用start()方法开始线程。
  • 实验问题分析:
    问题1:在输出运行结果时,总可以看到第一份作业被三个老师同时分发,而其后的作业没有出现这种错误。
    解决方案:在循环之前输出当前线程和分发作业数,并使作业书加1,错误消除。

2.模拟一个银行存款的程序。假设有两个储户都去银行往同一个账户进行存款,一次存100,每人存三次。要求储户每存一次钱,账户余额增加100,并在控制台输出当前账户的余额。

  • 程序设计思路:
    声明Bank类,构造方法,将存款进行更新。
    声明User类继承Runnable接口,重写run()方法,使用循环,使用synchronized(this)同步代码块,输出当前线程和存入后的存款。
    测试类,main()方法中声明一个User类对象,和两个Thread类线程作为用户,使用start()方法开始线程

(三)代码托管

  • 码云commit历史截图
posted @ 2017-06-01 18:11  Masart  阅读(152)  评论(0编辑  收藏  举报