Synchronization on immutable objects (in java)
问题 Code snippet - 1 class RequestObject implements Runnable { private static Integer nRequests = 0; @Override public void run() { synchronized (nRequests) { nRequests++; } } } Code snippet - 2 public class Racer implements Runnable { public static Boolean won = false; @Override public void run() { synchronized (won) { if (!won) won = true; } } } I was having a race condition with the first code snippet. I understood that this was because I was obtaining a lock on an immutable object(of type