| package com.Luoking.Thread; |
| |
| public class Race implements Runnable { |
| |
| private static String winner; |
| |
| |
| @Override |
| public void run() { |
| for (int i = 0; i <= 100; i++) { |
| |
| if(gameOver(i)){ |
| break; |
| } |
| if(Thread.currentThread().getName().equals("兔子")){ |
| try { |
| Thread.sleep(1); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| } |
| |
| System.out.println(Thread.currentThread().getName()+"走了"+i+"步"); |
| } |
| |
| } |
| |
| |
| private boolean gameOver(int steps) { |
| |
| if (winner != null) { |
| return true; |
| } |
| |
| else { |
| if (steps >= 100) { |
| 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(); |
| } |
| |
| } |
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决