• Java第十次作业


    (一)学习总结

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

    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接口的方式用多线程实现这个小游戏。下面给出主线程类,补充Tortoise线程类和Hare线程类。

    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);
            tortoiseThread.start();
            hareThread.start();
        }
    }
    

    补充:

    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);
            tortoiseThread.start();
            hareThread.start();
        }
    }
    
    class Hare implements Runnable{
        int hareStep=0;
        int totalStep=0;
        boolean[] flags = {true,false};
        boolean isHareSleep = flags[((int)(Math.random()*10))%2];
        public Hare(int totalStep){
            this.totalStep=totalStep;
        }
        public void run(){
            for(hareStep=0;hareStep<10;hareStep+=2){
                if(isHareSleep){
                    System.out.println("兔子睡着了zzzz");
                }else{
                    System.out.println("兔子跑了"+(hareStep+1)+"步...");
                }
            }
        }
    }
    
    class Tortoise implements Runnable{
        int tortoiseStep=0;
        int totalStep=0;
        public Tortoise(int totalStep){
            this.totalStep=totalStep;
        }
        public void run(){
            for(tortoiseStep=0;tortoiseStep<10;tortoiseStep++){
                System.out.println("乌龟跑了"+(tortoiseStep+1)+"步...");
            }
        }
    }
    

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

    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; 
        } 
    }
    

    修改:

    class Clerk {// 定义信息类
        private int clerk;
        private boolean flag = true; // 起始状态先生产后消费
    
        
        public synchronized void setProduct(int clerk) {
            // flag = false 无法生成
            while (this.flag == false) {
                
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            this.clerk = clerk;
            System.out.printf("生产者设定 (%d)%n", this.clerk); 
            this.flag = false;
            notify();
        }
    
        public synchronized int getProduct() {
            // flag = true 无法取走
             (this.flag == true) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            this.flag = true;
            notify();
            System.out.printf("消费者取走 (%d)%n",this.clerk);
            return clerk;
        }
    }
    

    (二)实验总结

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

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

    (三)代码托管
    https://git.oschina.net/hebau_cs15/Java-CS01QH.git

  • 相关阅读:
    监听本机tcp和udp的端口
    sysstat-----获取服务器负载历史记录
    inode索引详解
    tcpdump详解
    Windws Server 2008 R2 WEB环境配置之IIS7/IIS7.5+FastCGI+PHP 5.6.4+MYSQL+phpMyAdmin
    echo 命令
    带宽、流量、下载速度之间的换算
    windows 下解决 Time_Wait 和 CLOSE_WAIT 方法
    LNMP环境部署
    关于旅行
  • 原文地址:https://www.cnblogs.com/JoeH/p/6937497.html
Copyright © 2020-2023  润新知