aspose file tools*
The moose likes Threads and Synchronization and the fly likes synchronization on some instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "synchronization on some instance" Watch "synchronization on some instance" New topic
Author

synchronization on some instance

Susilo Saja
Ranch Hand

Joined: May 27, 2003
Posts: 91
hi guys, a question about synchronized
if i put the modifier synchronized on a non-static method, and some instance is created for the class that contain that method. if that method was called on EACH instance, will the synchronized modifier still take effect? or does the synchronized only work if we're calling that method on the same instance?
I'm not sure that my question is understoodable, pls tell me if it's not, thx
Susilo
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
In a synchronized block, the synchronization is always related to a single object.
Suppose you have a section of code that looks like this:

Since there are two different objects being used as two different locks, nothing at all prevents the two blocks from executing simultaniously. For protection, both blocks would need to synchronize on the same object instance. If you replace the line
final Object objectTwo = new Object();
with
final Object objectTwo = objectOne;
then the two blocks won't be able to execute at the same time, which is what synchronization is good for after all.
What's the object being used as the lock in a method with the synchronized modifier? For instance methods,

has the same behavior as


For a static method,

has the same behavior as


I don't feel like I did a great job with this, but I hope it was helpful.
Susilo Saja
Ranch Hand

Joined: May 27, 2003
Posts: 91
>>>In a synchronized block, the synchronization is always related to a single object.
Thanks David, I guess my question has been answered. But I can't understand at all about the synchronized block, especially about the object that being passed in:
synchronized (object) {
}
Tried to look at some different tutorial but can't understand a bit. Can you help me with that?
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
Here's a bit of code:

Here's a block which uses the code:

The output of that block could well look like this;
One: Before synchronization 1
Two: Before synchronization 1
One: Before synchronization 2
Two: Before synchronization 2
Two: Started some work...
One: Started some work...
One: ...working...
Two: ...working...
Two: Finishing the work...
Two: After synchronization 1
One: Finishing the work...
One: After synchronization 1
One: After synchronization 2
Two: After synchronization 2
As you can see, there's nothing at all stopping both instances of SynchronizedRunnable from executing the code in the synchronized block as the same time. The reason is that they're using two different locks. If you change the code to


Then you the lines "Started some work...", "...working...", and "Finishing the work..." will always appear right next to each other, because only on the the threads can hold the lock at any given time. Just another random sample output for this version:
Two: Before synchronization 1
One: Before synchronization 1
One: Before synchronization 2
Two: Before synchronization 2
Two: Started some work...
Two: ...working...
Two: Finishing the work...
One: Started some work...
One: ...working...
Two: After synchronization 1
One: Finishing the work...
One: After synchronization 1
One: After synchronization 2
Two: After synchronization 2
You can see that the synchronized sections do not overlap, although code outside the synchronized block may execute while other code is in the synchronized block (here that happened with "Two: After synchronization 1")
Susilo Saja
Ranch Hand

Joined: May 27, 2003
Posts: 91
Now I understand. Thanks!
 
wood burning stoves
 
subject: synchronization on some instance