多线程操作多实例的时候, 静态数据会有并发问题.
1:直接使用静态同步方法public class Foo { static int count = 0;
static public synchronized void inc( int i){ count = count + i;
} }
2:使用无关对象保护静态数据 public class Foo{ static int count = 0;
static Object o = new Object(); public void inc(int i){ synchronized
(o){ count = count + i ; } }}
3:使用Class Itself
保护静态数据public class Foo{ static int count = 0 ; public void inc(int
i){ synchronized(Foo.class){ count = count + i ;
} } }