java - getting NullPointerException when using syncrhonized on null object -
i have seen nullpointerexception on synchronized statement.
code:
synchronized(a){ = new a() } so according above answer i have understood not possible use synchronized keyword on null reference.
so changed code this:
synchronized(a = new a()){} but not sure if identical original code?
update:
what want achieve lock creation of a ( a = new a() )
synchronized(a = new a()){}
so create new object of class , use lock, in simple word every thread can enter in synchronized block anytime because each thread have new lock , there no other thread using object lock every thread can enter synchronized block anytime , outcome no synchronization
for example
class testclass { someclass somevariable; public void mymethod () { synchronized (somevariable) { ... } } public void myothermethod() { synchronized (somevariable) { ... } } } here can 2 blocks protected execution of 2 different threads @ time while somevariable not modified. basically, it's said 2 blocks synchronized against variable somevariable. but in case there new object there no synchronization
Comments
Post a Comment