java第十次作业

1.用思维导图对java多线程的学习内容进行总结。###

2.下面是一个单线程实现的龟兔赛跑游戏。阅读程序,采用实现Runnable接口的方式用多线程实现这个小游戏。下面给出主线程类,补充Tortoise线程类和Hare线程类。###

修改后的代码为:

     package shiyan;
     class Tortoise extends Thread {
    int tortoiseStep = 0;
    int totalStep;

    public Tortoise(int totalStep) {
        this.totalStep = totalStep;
    }

    public void run() {
        tortoiserun();
    }

    private synchronized void tortoiserun() {
        while (tortoiseStep < totalStep) {
            tortoiseStep++;
            System.out.println("乌龟跑了" + tortoiseStep + "步...");
        }
    }
}

class Hare extends Thread {
    int hareStep = 0;
    int totalStep;

    public Hare(int totalStep) {
        this.totalStep = totalStep;
    }

    public void run() {
        harerun();
    }

    private synchronized void harerun() {
        boolean[] flags = { true, false };
        while (hareStep < totalStep) {
            boolean isHareSleep = flags[((int) (Math.random() * 10)) % 2];
            if (isHareSleep) {
                System.out.println("兔子睡着了zzzz");
            } else {
                hareStep += 2;
                System.out.println("兔子跑了" + hareStep + "步...");
            }
        }
    }
}

public class Test extends Thread {
    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);
        tortoiseThread.start();
        hareThread.start();
    }
}

3.下面的程序是模拟了生产者——消费者问题,生产者生产10个数,消费者依次消费10个数,运行程序,看结果是否正常?存在什么问题?说明原因。使用synchronized, wait, notify解决程序出现的问题。写出修改的部分程序即可。###

执行的结果为:

生产者开始生产整数......
消费者开始消耗整数......
生产者设定 (1)
生产者设定 (2)
生产者设定 (3)
生产者设定 (4)
生产者设定 (5)
生产者设定 (6)
生产者设定 (7)
生产者设定 (8)
生产者设定 (9)
生产者设定 (10)
消费者取走 (10)
消费者取走 (10)

修改后的代码为:

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 表示目前没有产品 
private int call;
 // 这个方法由生产者呼叫
public synchronized  void setProduct(int product) {
     if (this.product != -1) {
         try {
             super.wait();
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
     }
    this.product = product; 
    System.out.printf("生产者设定 (%d)%n", call); 
    getProduct();
    this.product = -1;
    super.notify();
} 
// 这个方法由消费者呼叫
public synchronized  int getProduct() { 
     if (this.product == -1) {
         try {
             super.wait();
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
     }
    int call = this.product; 
    System.out.printf("消费者取走 (%d)%n", call);      
    this.product = -1;
    super.notify();
    return this.product;
} 
}

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

程序设计思路:实用implements接口资源共享,使用Runable方法,重写run方法,可以设置次数的局限性当控制条件,进行约束,三次即可。

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

程序设计思路;可以设置三个类别进行设置,一个事银行类,用户类,还有测试类,用户类是定义金钱方法,还有implements Runnable线程,和约束,可以选择金钱进行约束,当满足600时候停止,或者是次数进行约束,满足三次进行约束。
实验问题分析:实验问题1.如何将两个用户的金钱加到一个用户中去显示?
解决方案:不再用户类中解决金钱的问题,在他们统一的银行类中进行金钱的统一操作,manny+=100;

4.码云截图###

码云链接https://git.oschina.net/hebau_cs15/cg.git

原文地址:https://www.cnblogs.com/chengang4747/p/6928474.html