Can someone please explain the difference between these two examples in the context of object locking:
public void method1(){
synchronized(this){
....
}
}
And
StringBuffer aStringBufferObject = new StringBuffer("A");
public void method2(){
synchronized(aStringBufferObject){
....
}
}
I know the first example will obtain a lock on the this instance and the second will obtain a lock of the aStringBufferObject instance. But i dont really understand what the effect or the difference of the two is.
For example, in the second example, will threads still be able to execute the code inside the synchronized block because the lock is not related to the ‘this’ instance?
I know that synchronizing a method or a block of code prevents multiple threads to access that block/method at the same time but what is the purpose of specifying the object to lock on and what is the difference in the way the object is specified as in the above examples?
Often, it is easier to synchronize on
thisor on theClassinstance (for static methods). But, there are cases where you will need to synchronize on a specific object instead of the implicit lock (this). Such cases include:this. You can only synchronize onObjects as eachObjectis associated with an implicit monitor in Java. Primitives do not have such implicit monitors, and therefore you need to use a lock object. Using the wrapper classes are a poor and incorrect choice, especially if you end up modifying the lock object in the guarded block.thiswould not offer a guarantee of thread safety. For instance, if you are synchronizing access to aArrayListinstance shared across instances of classA, then synchronizing on an instance ofAis useless. A thread might create a new instance ofAand gain access to the list, while another thread is modifying it. If you use a different lock that all threads must contend for then you can protect the list; this lock could be the one associated withA.class, but it could be any object that will offer the same guarantees.The following is an example of split lock usage:
You would use split locks when you can ensure that the concurrent execution of
method1andmethod2do not violate the class invariants. This way, you can improve performance across threads that need access to the same object, but will be invoking different methods.On your other question,
In the second example, any thread entering the guarded region must acquire the lock associated with
aStringBufferObject. If a different thread is holding that lock, then the current thread will not proceed further. When you specifythis, then the thread must acquire the lock associated with the current object. In both cases, a thread must acquire a lock; the examples are only different in the object that is being used as a lock.