本帖最后由 罗正荣 于 2013-2-28 21:22 编辑
- class Ticket implements Runnable
- {
- private static int tick = 100;
- boolean flag = true;
- public void run()
- {
- if(flag)
- {
- while(true)
- {
- synchronized(Ticket.class)
- {
- if(tick>0)
- {
- try{Thread.sleep(10);}catch(Exception e){}
- System.out.println(Thread.currentThread().getName()+"....code : "+ tick--);
- }
- }
- }
- }
- else
- while(true)
- show();//这里应该是省略了Ticket.这样才能起到同步的作用,但是我感觉这个
- //show难道不是对象在调用吗?为什么是Ticket类在调用呢?
- }
- public static synchronized void show()
- {
- if(tick>0)
- {
- try{Thread.sleep(10);}catch(Exception e){}
- System.out.println(Thread.currentThread().getName()+"....show.... : "+ tick--);
- }
- }
- }
- public class StaticMethodDemo
- {
- public static void main(String[] args)
- {
- Ticket t = new Ticket();
- Thread t1 = new Thread(t);
- Thread t2 = new Thread(t);
- t1.start();
- try{Thread.sleep(10);}catch(Exception e){}
- t.flag = false;
- t2.start();
- }
- }
复制代码 问题就是代码中的注释?求解 |