练习二:创建三个窗口卖票,总票数为100张,使用继承Thread类的方式
1 package com.atfu.java01; 2 3 /** 4 * 例子:创建三个窗口卖票,总票数为100张,使用继承Thread类的方式。 5 *存在线程安全问题,待解决。 6 * 7 * 8 * 9 * 10 * @author fu jingchao 11 * @creat 2021/10/15-16:16 12 */ 13 class WindoW extends Thread{ 14 private static int ticket = 100; 15 @Override 16 public void run() { 17 while (true){ 18 if(ticket >0){ 19 System.out.println(getName() + ":卖票,票号为:" +ticket); 20 ticket--; 21 }else { 22 break; 23 } 24 } 25 } 26 } 27 28 public class WindowTest { 29 public static void main(String[] args) { 30 WindoW w1 = new WindoW(); 31 WindoW w2 = new WindoW(); 32 WindoW w3 = new WindoW(); 33 w1.setName("窗口一"); 34 w2.setName("窗口二"); 35 w3.setName("窗口三"); 36 w1.start(); 37 w2.start(); 38 w3.start(); 39 } 40 }
此为本人学习笔记,若有错误,请不吝赐教