How to synchronize a static variable among threads running different instances of a class in Java?
There are several ways to synchronize access to a static variable. Use a synchronized static method. This synchronizes on the class object. public class Test { private static int count = 0; public static synchronized void incrementCount() { count++; } } Explicitly synchronize on the class object. public class Test { private static int count … Read more