java - Would changes be visible to a different thread if synchronizing using different objects? -
imagine piece of code inside class. trying change val
value in different threads calling method1
, method2
, respectively.
is expected changes made val
visible in thread, if using different objects synchronize on? , test case design?
private object lock1 = new object(); private object lock2 = new object(); private int val = 0; public void method1 () { synchronized (lock1) { system.out.println(val); val = 1; } } public void method2 () { synchronized (lock2) { system.out.println(val); val = 2; } }
there no happens-before relation between acquiring , releasing different monitors, there no guarantee when changes visible.
Comments
Post a Comment