- class Maipiao implements Runnable
- {
- private int piao=100;
-
- public /*synchronized*/ void run()
- {
-
-
- Object a=new Object();
-
-
- while(true)
- {
- synchronized(a)
-
- { if(piao>0)
- {
- try{Thread.sleep(10);}catch (InterruptedException e){}
-
- System.out.println(Thread.currentThread().getName()+"piao="+piao--);
-
- }
- }
-
- }
- }
- }
- class Demo
- {
- public static void main(String[] args)
- {
- Maipiao a=new Maipiao();
- Thread b=new Thread (a);
- Thread c=new Thread (a);
- Thread d=new Thread (a);
- Thread e=new Thread (a);
- b.start();
- c.start();
- d.start();
- e.start();
- System.out.println("Hello World!");
- }
- }
复制代码
大家看一下,结果运行出来怎么还有0,-1,如果改成同步函数就没有这个问题。大家看一下 |
|