• 2020年9月11日 编写龟兔赛跑的多线程程序


    * 2、案例:编写龟兔赛跑多线程程序,设赛跑长度为30米
    兔子的速度是10米每秒,兔子每跑完10米休眠的时间10秒
    乌龟的速度是1米每秒,乌龟每跑完10米的休眠时间是1秒
    要求,
    (1)每跑1米,显示一下结果:xxx跑了几米,
    休息时,显示一下:xxx在休息...

    (2)只要有跑完的,比赛就结束,最先跑完的就赢了,其他运动员就停下来别跑了
    相当于,只比冠军

    public class TestExer08 {
        public static void main(String[] args) {
            //1、启动线程
            Sportman t = new Sportman("兔子",30,100,10000);
            Sportman w = new Sportman("乌龟",30,1000,1000);
            
            t.start();
            w.start();
            
            //2、判断是否有运动员跑完了
            while(true){
                //如果有人跑完了,停下所有线程
                if(t.isFinish() || w.isFinish()){
                    t.interrupt();
                    w.interrupt();
                    
    /*                t.stop();
                    w.stop();*/
                    
                    t.setStop(true);
                    w.setStop(true);
                    
                    break;
                }
            }
            
            //3、宣布结果
            //看谁先跑完,谁就赢了
            if(t.isFinish() && w.isFinish()){
                System.out.println(t.getName() + "," + w.getName() + "平局");
            }else if(t.isFinish()){
                System.out.println(t.getName() + "赢了");
            }else{
                System.out.println(w.getName() + "赢了");
            }
        }
    }
    class Sportman extends Thread{
        private int distance;//距离
        private long runMillsPerMeter;//每米的时间,毫秒
        private long restMillsPerTenMeter;//每10米休息的时间,毫秒
        private long totalTime;
        private volatile boolean finish;//默认值是false,如果跑完了,修改为true
        private boolean stop;//默认值是false
        
        public Sportman(String name, int distance ,long millsPerMeter, long restPerTenMeter) {
            super(name);
            this.distance = distance;
            this.runMillsPerMeter = millsPerMeter;
            this.restMillsPerTenMeter = restPerTenMeter;
        }
    
        public void run(){
            long start = System.currentTimeMillis();
            int i;
            for (i = 1; i <= distance && !stop; i++) {
                try {
                    Thread.sleep(runMillsPerMeter);//模拟跑1米的时间
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //用线程名称代替运动员的名称
                System.out.println(getName() + "跑了" + i + "米");
                
                if(i<distance && i%10==0){
                    System.out.println(getName() + "在休息....");
                    try {
                        Thread.sleep(restMillsPerTenMeter);//休息n秒
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                
            }
            long end = System.currentTimeMillis();
            totalTime =  end - start;
            if(i >= distance){//不是中途结束,而是全程跑完的
                finish = true;
            }
        }
    
        public long getTotalTime() {
            return totalTime;
        }
    
        public boolean isFinish() {
            return finish;
        }
    
        public void setStop(boolean stop) {
            this.stop = stop;
        }
        
    }
    package com.guigu.test07;
    
    public class TestExer7 {
        public static void main(String[] args) {
            SportMan tuzi = new SportMan("兔子",30,100, 10000);
            SportMan wugui = new SportMan("乌龟",30, 1000, 1000);
            tuzi.start();
            wugui.start();
            
            while(true){
                if(tuzi.isFinish() || wugui.isFinish()){
                    tuzi.interrupt();
                    wugui.interrupt();
                    
    /*                tuzi.stop();
                    wugui.stop();*/
                    
                    tuzi.setStop(true);
                    wugui.setStop(true);
                    
                    
                    break;
                }
            }
    
            if(tuzi.isFinish() && wugui.isFinish()){
                System.out.println("同时到达");
            }else if(tuzi.isFinish()){
                System.out.println(tuzi.getName()+"赢了");
            }else{
                System.out.println(wugui.getName()+"赢了");
            }
            
        }
    }
    class SportMan extends Thread{
        private int distance;
        private long milesperMeters;
        private long restperMeters;
        private long totalTime;
        private volatile boolean finish;
        private boolean stop;
        
        
        public SportMan(String name,int distance, long milesperMeters, long restperMeters) {
            super(name);
            this.distance = distance;
            this.milesperMeters = milesperMeters;
            this.restperMeters = restperMeters;
        }
    
        @Override
        public void run() {
            long start = System.currentTimeMillis();
            int i;
            for (i = 1; i <= distance && !stop; i++) {
                try {
                    //速度-米每秒
                    Thread.sleep(milesperMeters);//模拟跑1米的时间
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(getName()+"跑了" + i + "米");
                
                if(i<distance && i%10==0){
                    System.out.println(getName()+"在休息....");
                    try {
                        Thread.sleep(restperMeters);//休息restperMeters秒
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                
            }
            if(i >= distance){//不是中途结束,而是全程跑完的
                finish = true;
            }
            long end = System.currentTimeMillis();
            totalTime = end - start;
            
            
        }
    
        public long getTotalTime() {
            return totalTime;
        }
    
        public boolean isFinish() {
            return finish;
        }
    
        public void setStop(boolean stop) {
            this.stop = stop;
        }
        
    }
  • 相关阅读:
    kubeadm安装kubernetes集群
    推荐几个大厂的前端代码规范,你也能写出诗一样的代码!
    恕我直言,你可能连 GitHub 搜索都不会用
    全球最火的WEB开发学习路线!没有之一!3 天就在GitHub收获了接近 1w 点赞
    VS2013扩展和更新JS智能提示
    linux下升级git版本的操作记录(摘录)
    python 多版本 安装模块 ModuleNotFoundError: No module named 'xxx'
    记录一次app报病毒的问题
    python 根据大图片生成各种规格图片 生成ios需要的各个规格的icon
    React Native iOS 项目初始化
  • 原文地址:https://www.cnblogs.com/douyunpeng/p/13652973.html
Copyright © 2020-2023  润新知