用在多线程中。比如:MyThread类继承于Thread,我们要用for(;;)循环来创建MyThread类100次。在MyThread类有个线程计数器(int),创建MyThread时加1,退出时减1,这个计数器不可能同时又加又减,synchronized就是用来实现这个功能的。
如上例,当运行到 synchronized  (Class.forName("BumpTest"))  {
                  classCount++;
            }时其它线程不能对classCount进行操作,只有程序运行完synchronized...{...}后其它线程才能操作classCount,同时只能有一个线程操作classCount。 

解决方案 »

  1.   

    csdn_cloud(拔光毛的兔兔) 说得很经典
      

  2.   

    to GJA106(中文字符) 对静态的变量来说用同步很有必要的
    但是对于一般;的变量,而且由于只有一个函数对他操作没必要用到同步呀!!      synchronized  void  bump()  {  count++;  }//为什么同步呀??
      

  3.   

    For a class(static) method, we don't need to create instance for the class, so the lock associated with the Class objectfor  the  method's class is used. It work like "synchronized(Class.forName("BumpTest"))".