package chapter2; /** * 1.使用static虽然完成了当前的功能。 * 但是static的生命周期比较长,需要在所有线程执行完成后才会结束。 * 还有号没有按照顺序进行打印 */ public class TicketWindow extends Thread { private static final int MAX =50; private final String name ; private static int index=1; public TicketWindow(String name) { this.name = name; } @Override public void run() { //出票 while (index<=MAX){ System.out.println("当前柜台:"+name+"当前号码是:"+(index++)); } } }
package chapter2; public class Bank { public static void main(String[] args) { TicketWindow tick = new TicketWindow("柜台一"); tick.start(); TicketWindow tick2 = new TicketWindow("柜台二"); tick2.start(); TicketWindow tick3 = new TicketWindow("柜台三"); tick3.start(); TicketWindow tick4 = new TicketWindow("柜台四"); tick4.start(); } }