zoukankan      html  css  js  c++  java
  • java多线程模拟龟兔赛跑

    让乌龟和兔子在同一个赛道从1开始跑到100,看看谁更快.

     1 public class Racer implements Runnable{
     2     private static String winner;//胜利者
     3 
     4     @Override
     5     public void run() {
     6         //赛道
     7         for (int step = 1; step <= 100; step++) {
     8  /* if(Thread.currentThread().getName().equals("兔子")&&step%50==0){
     9       try {
    10           Thread.sleep(200);
    11       } catch (InterruptedException e) {
    12           e.printStackTrace();
    13       }
    14   }*/
    15             System.out.println(Thread.currentThread().getName()+"---->走了"+step+"步");
    16 
    17              boolean flag= GameOver(step);
    18              if(flag){
    19                  break;
    20              }
    21 
    22         }
    23     }
    24 
    25     private boolean GameOver(int step) {
    26         if(winner!=null){
    27             return true;
    28         }else{
    29             if(step==100){
    30                 winner=Thread.currentThread().getName();
    31                 System.out.println("胜利者是-->"+winner);
    32             }
    33         }
    34         return false;
    35     }
    36 
    37     public static void main(String[] args) {
    38         Racer racer = new Racer();
    39         new Thread(racer,"兔子").start();
    40         new Thread(racer,"乌龟").start();
    41 
    42     }
    43 
    44 }

    运行结果:

    兔子---->走了1步

    ...................

    兔子---->走了98步
    兔子---->走了99步
    兔子---->走了100步
    胜利者是-->兔子

    怎么竟然是兔子赢了,可是现实中龟兔赛跑是乌龟赢了,我们加入线程睡眠要是兔子就让他睡一会,乌龟就可以赢了.

     1 public class Racer implements Runnable{
     2     private static String winner;//胜利者
     3 
     4     @Override
     5     public void run() {
     6         //赛道
     7         for (int step = 1; step <= 100; step++) {
     8   if(Thread.currentThread().getName().equals("兔子")&&step%50==0){
     9       try {
    10           Thread.sleep(200);
    11       } catch (InterruptedException e) {
    12           e.printStackTrace();
    13       }
    14   }
    15             System.out.println(Thread.currentThread().getName()+"---->走了"+step+"步");
    16 
    17              boolean flag= GameOver(step);
    18              if(flag){
    19                  break;
    20              }
    21 
    22         }
    23     }
    24 
    25     private boolean GameOver(int step) {
    26         if(winner!=null){
    27             return true;
    28         }else{
    29             if(step==100){
    30                 winner=Thread.currentThread().getName();
    31                 System.out.println("胜利者是-->"+winner);
    32             }
    33         }
    34         return false;
    35     }
    36 
    37     public static void main(String[] args) {
    38         Racer racer = new Racer();
    39         new Thread(racer,"兔子").start();
    40         new Thread(racer,"乌龟").start();
    41 
    42     }
    43 
    44 }

    运行结果:

    乌龟---->走了96步
    乌龟---->走了97步
    乌龟---->走了98步
    乌龟---->走了99步
    乌龟---->走了100步
    胜利者是-->乌龟
    兔子---->走了50步

    这下不管怎么跑都是乌龟赢了.

  • 相关阅读:
    维度穿梭
    演绎与抽象
    幻想的功能
    深层探宝
    内存游戏
    函数内功
    共享与私有的变量
    参数的格式
    功能模拟与功能实现
    【Oracle】基础知识查漏补缺
  • 原文地址:https://www.cnblogs.com/xiaoqiqistudy/p/10984279.html
Copyright © 2011-2022 走看看