多线程龟兔赛跑(Java)
发布日期:2021-05-10 10:17:20 浏览次数:12 分类:精选文章

本文共 4830 字,大约阅读时间需要 16 分钟。

< html > < div > < head > < title > ��������������������������������������������� </ title > </ head > < div > < body > < h2 > ��������������������������������������������� </ h2 >

������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

������������������������������������������������������������������������������������������ thread ��������������� runnable ��������������� Java ��� Thread ��������������������������������� runnable ������������������������ thread ������������������������������������������ runnable ��������������������� thread ������������������������������������������������ runnable ������������������������������������������ thread������������������������ thread ������������������������ runnable ������������������������������������

��������������������������������������������������������� thread ��������������������������������������� Race ������������������ Thread��������������� runnable ������������������������������������������������������������ instance of Race������������������ thread ������������������������������ runnable ���������������������

������������������������������������

package thread.demo1;
public class Race implements Runnable {
private static String winner;
@Override
public void run() {
boolean flag;
for (int i = 1; i <= 1000; i++) {
if (Thread.currentThread().getName().equals("������") && i % 5 == 0) {
try {
System.out.println("������������");
Thread.sleep(1);
i += 5;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
flag = gameOver(i);
if (flag) {
//���������������������������������������
break;
}
System.out.println(Thread.currentThread().getName() + "������" + i + "���");
}
}
public boolean gameOver(int dist) {
if (winner != null) {
return true;
} else {
if (dist >= 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();
}
}

������������������������������������������������������������

  • ������������������������������ runnable ��������������������� thread ��������������� thread ������������������������ runnable ������������������������������������������������������������������������ thread ���������������������������������

  • ������������������������������������������������ thread ��������������������� runnable ������������������������������������������������������������������������������

  • ������������������������������������ thread ������ 1000 ��������������������������������������������������������������������������� thread ������������������������������������������������������

  • ���������������������Java ��� Thread ���������������������������������������������������������������������������������������������������������

  • ������������������������������������������������������������

  • ��������������� runnable ��������������� runnable ��������������������������������������������������������� thread ���������������������������������

  • ��������synchronized ��������������������������������������������������������������������������� synchronized ������������������������������ thread ������������������ runnable ������������������

  • ������ sleeping ��������������������� thread.sleep() ���������������������������������������������������������������

  • ������ gameOver ������������ setting winner ��������������������������� loop ���������������������������������������������������

  • ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������� runnable ��������������������� thread ������������������������������������������������������������������������������������������������

    ��������������������������������������������������������������������������������������������������������� thread pools��������������������������������� thread������������������ runnable ������������������������������������������������������������������������

    ������������������������������������������������������������������������������������������������������������������

    上一篇:多线程不安全案例(不安全&synchronized同步解决):买票,银行取钱
    下一篇:Dijkstra算法解析(java+图解)

    发表评论

    最新留言

    关注你微信了!
    [***.104.42.241]2025年04月08日 15时02分14秒