| Author |
confusion about object lock.
|
pawan chopra
Ranch Hand
Joined: Jan 23, 2008
Posts: 362
|
|
Hi All,
I have a class as shown below:
this class has two methods they are synchronized. Now let us say thread 1 is executing firstMethod while its executing that thread 2 tries to execute the second method on same object. Will it be able to execute it or not? If yes then how come this is lock on a object? I hopw this is not a silly question. Please help.
|
Pawan Chopra
SCJP - DuMmIeS mInD
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
|
When a synchronized method of an object is called, all other synchronized of the same object will block until the executed method ends.
|
[My Blog]
All roads lead to JavaRanch
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
Here is an example where the first method sleeps 5 seconds. Check what happens with and without the synchronized keyword on the second method.
|
 |
pawan chopra
Ranch Hand
Joined: Jan 23, 2008
Posts: 362
|
|
Thanks a lot Christophe!
|
 |
 |
|
|
subject: confusion about object lock.
|
|
|