You are here:

Java/new doubt on synchronized block

Advertisement


Question
HI sir!Do noty think I am asking repeated question.In previous times I could not express my doubt success fully.so once again i am sending
----------------------------
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:
1)why should we lock an object in table class other than table class in part2?
2)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?
3)Will object of customer class try to access the synchronized block simultaneously from different places?
4).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?
5)What is the need to lock object of customer class i.e other than table class?
6)What damage will happen if we do not get lock on object of customer class in part2?
--------------------------------------

I think meaning of all questions is same.But I expressed doubts as I got.
I hope you answer

Answer
Hi,

I honestly, don't know where you are getting at with your questions as I don't understand them..but I will try my best to explain..

First, if in part 1 you got lock on a class object, that might mean something like you have a lock on the class. Then all objects of that class is locked.  Why would you want to do that? perhaps in a multifaced project with so many classes.. That means, that until the thread with the lock releases it, ALL the threads on the other instances of the class objects cannot access the given resources(it could be a method in this case..)

Secondly, if in part 2, the lock is on a method that means, it has no "Static" keyword attached to it. It is an instance object and not a class object.. only the threads of the object concerned is affected. Please, try to go to the oracle document to study these further.  The situation changes depending on what you are doing. the simple straight fact is, IF you lock a resource, you have to release it for it to be used by other objects. From that simple fact, you can start creating your bases for other scenario..

Best of lucks..

Java

All Answers


Answers by Expert:


Ask Experts

Volunteer


henry joe

Expertise

I can confidently answer questions relating to JSE and JEE6 . Please no questions on JME.

Experience

I am experienced in JSE and JEE6

Education/Credentials
I am a BSEng graduate

©2016 About.com. All rights reserved.