欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

龟兔赛跑(多线程)

程序员文章站 2022-05-02 10:05:42
...

能更好的的理解多线程

package Demo11Thread;

import java.util.SortedMap;

/**
 * 龟兔赛跑
 */
public class Race implements Runnable{
    private static String winner;

    @Override
    public void run() {
            for (int i = 0; i <= 1000; i++) {
                //模拟兔子休息,每十步休息一次
                if (Thread.currentThread().getName().equals("兔子") && i%10 ==0) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //判断比赛是否结束
                boolean flag = Win(i);
                if (flag){
                    break;
                }
                System.out.println(Thread.currentThread().getName() + "跑了" + i + "步");
            }
    }
    public boolean Win(int steps) {

        if (winner != null) {
            return true;
        }
            {
            if(steps >= 1000){
            winner = Thread.currentThread().getName();
            System.out.println("winner is" + winner);
            return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Race race= new Race();

       new Thread(race,"兔子").start();
       new Thread(race,"乌龟").start();

    }
}

运行结果

兔子每10步休息一次。Thread.sleep

龟兔赛跑(多线程)
龟兔赛跑(多线程)

相关标签: java 多线程