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
Post a Comment