当前位置:网站首页>Tortoise and rabbit race example
Tortoise and rabbit race example
2022-06-26 05:59:00 【Mr.Rop】

// A race between the hare and the tortoise
public class Race implements Runnable {
private static String winner;
@Override
public void run() {
for (int i = 0; i <= 1000; i++) {
// Simulated rabbit sleeping
// if (Thread.currentThread().getName().equals(" The rabbit ")&&i%400==0){
// try {
// Thread.sleep(1);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
boolean flag = gameOver(i);
if (flag){
break;
}
System.out.println(Thread.currentThread().getName()+"---> ran "+i+" Step ");
}
}
private boolean gameOver(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," The rabbit ").start();
new Thread(race," Tortoise ").start();
}
}
边栏推荐
猜你喜欢

Household accounting procedures (First Edition)

Getting started with Python

小程序如何关联微信小程序二维码,实现二码聚合

423- binary tree (110. balanced binary tree, 257. all paths of binary tree, 100. same tree, 404. sum of left leaves)

Overloading and overriding

REUSE_ALV_GRID_DISPLAY 事件实现(DATA_CHANGED)
转帖——不要迷失在技术的海洋中

Gram matrix

On site commissioning - final method of kb4474419 for win7 x64 installation and vs2017 flash back
Posting - don't get lost in the ocean of Technology
随机推荐
E-commerce seeks growth breakthrough with the help of small program technology
numpy.tile()
05. basic data type - Dict
Feelings of virtual project failure
Some doubts about ARP deception experiment
小程序如何关联微信小程序二维码,实现二码聚合
RIA想法
工厂方法模式、抽象工厂模式
Gram matrix
MySQL-08
Describe an experiment of Kali ARP in LAN
"= =" difference from "equals"
The difference between abstract and interface interface
Easy to understand from the IDE, and then talk about the applet IDE
从新东方直播来探究下小程序音视频通话及互动直播
Matching environment of ES6
Getting started with Python
Application of cow read / write replication mechanism in Linux, redis and file systems
Written before father's Day
String类学习