- class Ticket extends Thread
- {
- private int tick = 100;//为什么这里static之后就变成只卖100张票而不是400张票啊?
- public void run()
- {
- while(true)
- {
- if(tick>0)
- {
- System.out.println(Thread.currentThread().getName()+"....sale : "+ tick--);
- }
- }
- }
- }
- class TicketDemo
- {
- public static void main(String[] args)
- {
- Ticket t = new Ticket();
- Thread t1 = new Thread(t);
- Thread t2 = new Thread(t);
- Thread t3 = new Thread(t);
- Thread t4 = new Thread(t);
- t1.start();
- t2.start();
- t3.start();
- t4.start();
- }
- }
复制代码 |