This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread" Watch "Thread" New topic
Author

Thread

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
Hello!

It is said:

When a thread is executing code from within a synchronized block, including
any method code invoked from that synchronized block, the code is said to be
executing in a synchronized context. The real question is, synchronized on what? Or,
synchronized on which object's lock?
When you synchronize a method, the object used to invoke the method is the
object whose lock must be acquired. But when you synchronize a block of code, you

specify which object's lock you want to use as the lock, so you could, for example,
use some third-party object as the lock for this piece of code. That gives you the
ability to have more than one lock for code synchronization within a single object.

Or you can synchronize on the current instance (this) as in the code above.
Since that's the same instance that synchronized methods lock on, it means that
you could always replace a synchronized method with a non-synchronized
method containing a synchronized block.


May someone give me example for this part:
, for example,
use some third-party object as the lock for this piece of code. That gives you the
ability to have more than one lock for code synchronization within a single object.

Thanks!
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
There are many people that use Java to connect to databases, and do other work. There are many places where this would come up, but I suggest that as a beginner you just remember that code can synchronize of a variety of Objects.

Synchronization can lock if you try to do this:



Are you just studying, or do you have a real coding context for the question.
[ October 03, 2007: Message edited by: Nicholas Jordan ]

"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
Hello

I'm learning,

May you give me complete example for that?
Thanks
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
It gets pretty involved, just keep going with what you have and get back with us if you have difficulties.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread
 
Similar Threads
synchronized block
Synchronized methods Vs Synchronized blocks
locks threads
Thread
Difference between synchronized method and synchrozed block