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

How do you change vbulletin's home page to the forums instead of the default (CMS or Activity Stream)? -

c++ - Troubles when compiling phash program -