Hi! ranchers i have a little question- if i synchronize static block on instance it runs fine, but is that different from getting lock of "Class" object as static methods holds by default for eg: class a{ static void o() {
synchronized(new a()){ // 1 }
}
} i mean to say am i getting lock on class represented by newly created instance at line 1.
i mean to say am i getting lock on class represented by newly created instance at line 1.
You are synchronizing with the instance of class A that you created at line #1. And since, it is impossible for any other thread to get access to that lock -- you don't save a reference to the object anywhere -- it pretty much doesn't do anything useful.
To give a further example of locking with the "Class" object, and synchronization of static methods... All three of these methods are synchronizing on the same lock...