本帖最后由 @在路上 于 2014-12-2 22:16 编辑
- class MaiPiao implements Runnable// extends Thread
- { private int ticket =100; //static 生命周期过长
-
- public void run()
- {
- while (true)
- { synchronized(this)
- {
- if(ticket>0)//子类抛出的异常类型不能比父类抛出的异常类型更宽泛 因为是接口
- try{Thread.sleep(10);}catch(Exception e){} //将这行注释才不会出现负票求解
- System.out.println(Thread.currentThread().getName()+"sale:"+ticket-);
- //Thread.currentThread中的Thread类为何能不能省略
- }
- }
- }
- }
- class TicketDemo
- {
- public static void main(String[] args)
- {
- MaiPiao p=new MaiPiao();
- Thread t1=new Thread(p);
- Thread t2=new Thread(p);
- Thread t3=new Thread(p);
- Thread t4=new Thread(p);
- t1.start();
- t2.start();
- t3.start();
- t4.start();
- }
- }
复制代码 刚又看了下是if()没加{} :L
|