multithreading - Can I check for null on a shared field outside of a synchronized block in java? -


let's have field can accessed 2 separate threads. i'm using object synchronization lock. can check null outside of synchronization block? in other words, thread-safe:

private object sharedobject() = new object(); private final object sharedobjectlock() = new object();  private void awesomemethod() {    if(sharedobject != null) {       synchronized(sharedobjectlock) {          //code uses sharedobject       }    } } 

no. assignment of variable occur after check before lock.

there was, @ 1 time, school of thought synchronized locks expensive, that's not true anymore. grab lock.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -