You are here:

Java/new doubt on synchronized block

Advertisement


Question
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
1. There are a couple of reasons. First you may want to prevent other code from accessing the hidden lock to prevent it from interfering with your own locking. Second, you may want multiple locks in cases there are separate operations that can run at the same time, but not with others.
2. You don't just prevent the table class from access, you prevent all code from accessing a synchronized block when a thread is using it. The actual object doesn't matter as long as it is the same object used in the lock entry into the synchronization block.
3. See #2
4. see #1
5. see #1
6. This depends on what the code is doing. For example, if code to update an array was:
#Place item at end of list.
#Update size of list.
Then if two threads execute the block at the same time, it is possible, but unlikely that:
#Thread 1 places an item at end of list.
#Thread 2 places an item at end of list, which overwrites what thread 1 placed.
#Thread 2 updates size of list.
#Thread 1 updates size of list, which doesn't do anything since the list size was already updated.
In this case, both try to write an object to the list, but only 1 gets written.

Java

All Answers


Answers by Expert:


Ask Experts

Volunteer


Artemus Harper

Expertise

I have a Masters in computer science. I can answer questions on core J2SE, swing and graphics. Please no questions about JSP or J2ME.

Experience

I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection. Some experience in bio-informatics. Basics in c++ and c#

Organizations
Washington State University

Education/Credentials
MS in Computer Science from Washington State University and a BS in Mathematics and Computer Science from Central Washington University.

©2016 About.com. All rights reserved.