Reputation: 2074
Please, I have doubts between a synchronized block and a synchronized method with the below scenario:
Part1 --------------:
Class table
{
Synchronized(this)
{
……………………
}
}
Here we got lock on object of table class. --------------------------------------…
Part2:
Class table
{
Customer cust=new Customer();
Synchronized(cust)
{
……………………
}
}
Here we got lock on object of customer.i.e on cust
In part1 we got lock on object of?- table class.
We get lock on table class because object of table class may try to access the synchronized
block from different places simultaneously.To prevent it we got lock on object of table class
in part1.
Now come to part2:?-
Here we got lock on object of customer class
--------------------------------------…
My doubts:
why should we lock an object in table class other than table class in part2?
If we do not get lock on cust in part 2 then we can access synchronized block using table
class
If we lock cust then also we need object of table class to access synchronized block.
Object of table class only needed to access synchronized block.
Due to synchronization we prevented object of table class to access synchronized block
simultaneously from different threads.
Then why should we lock object other than table class in table class?
Will object of customer class try to access the synchronized block simultaneously from
different places?
In part2 we got lock on object other than table class.
In What situations getting lock in table class on an object of a class other than table is
benefit of getting lock on object of customer class in part2?
What is the need to lock object of customer class i.e other than table class?
What damage will happen if we do not get lock on object of customer class in part2?
Please, I am new to Java thread and I have been trying to wrap my head around those questions for a while. Any clear explanation will help alot. Thanks
Upvotes: 0
Views: 201
Reputation: 244
It seems you analyze some existing code and try to understand why there is synchronization.
Are the part1 and part2 code in the same function?
In the second part the synchronization does not make sense. Creating the customer object and then synchronizing has no effect. A second thread would create a new customer object and lock on that object.
If you need to change existing code you can write a testcase and span may threads and remove the synchronization to see if you can force a multithreading issue.
Upvotes: 0
Reputation: 16335
First thing you should check out is : What kind of DATA fields will be protected using "synchronized" keyword for different types.
static variables/methods are class level objects and all other variables/methods are instance level.
A class level lock is the lock which makes all object of a class to wait until the corresponding lock is not released.
e.g.
Class A{
static synchronized void foo(){}
}
Here the method foo is synchronized and hence all the threads on all the objects of the class will wait until the object currently running the foo method completes its execution.
Similarly an instance level lock makes all the threads started using the instance of the class to wait until the lock is not released.
e.g.
Class A{
synchronized void bar(){}
}
Here all the threads started from the object which is currently executing the bar method will wait until the current threads completes its execution. Note that other threads of other objects can execute the bar method while another object's thread is executing the bar method.
In other words, a thread cannot enter a synchronized block of code until it holds the appropriate object lock.
For instance methods
, this is the lock of an instance.
For static methods
this is the Class object lock.
Upvotes: 1