public class Test { |
public static void main(String[] args) { |
TestThread t = new TestThread(); |
// 7个售票窗口 |
new Thread(t).start(); |
new Thread(t).start(); |
new Thread(t).start(); |
new Thread(t).start(); |
new Thread(t).start(); |
new Thread(t).start(); |
new Thread(t).start(); |
} |
} |
class TestThread implements Runnable { |
private int tickets = 20 ; |
public void run() { |
while ( true ) { |
synchronized ( this ) { |
if (tickets > 0 ) { |
try { |
Thread.sleep( 100 ); |
} catch (Exception e) { |
} |
System.out.println(Thread.currentThread().getName() |
+ "售票, 余票" + --tickets); |
} else { |
System.out.println( "票已售完..." ); |
System.exit( 0 ); |
} |
} |
} |
} |
} |