0%
利用主线程控制子线程
通知
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| package windowSale;
public class WindowSale1 { public static int countTicket=10;
public static void main(String[] args) { Window1 window1 = new Window1(); Thread thread=new Thread(window1); thread.start(); while (true){ if (countTicket<=5){ System.out.println("小于5张,请停止售票"); window1.setJudge(false); break; } else {System.out.println("不要停下来");} try{ Thread.sleep(50); }catch (InterruptedException interruptedException){ interruptedException.printStackTrace(); } }
} } class Window1 implements Runnable{ private boolean judge=true; @Override public void run() { while (judge){ try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"售卖一张票"); WindowSale1.countTicket=WindowSale1.countTicket-1; System.out.println("还剩"+WindowSale1.countTicket); } }
public void setJudge(boolean judge) { this.judge = judge; } }
|