aspose file tools*
The moose likes Threads and Synchronization and the fly likes Synchronized blocks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Synchronized blocks" Watch "Synchronized blocks" New topic
Author

Synchronized blocks

Zarko Bucic
Greenhorn

Joined: May 25, 2006
Posts: 3
Guys I am a litle confused with synchronized blocks, example:

where B is also some object with synchronized blocks and methods.

So a question is:
On which object, active thread, get a lock when invoke synchronized block of someMethod() in A???
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
In your example the current thread would have to acquire the monitor for the object that the reference "B" points to.
Zarko Bucic
Greenhorn

Joined: May 25, 2006
Posts: 3
Thank you Ken for your reply,
but is that mean, that a lock of object A will still be free, regardless if any thread enters into synchronized block?
Chris Beckey
Ranch Hand

Joined: Jun 09, 2006
Posts: 116

It means that where code is synchronized on the object that B references, then only a single thread will execute any of those blocks at any one time.
Every instance (of every class) has a monitor associated with it. Synchronizing ( as in synchronized(X) or public void synchronized X() ) associates that monitor object to a thread (locks it). Only a single thread can lock the monitor. If the thread cannot get the monitor then it waits.

For example:
public class A{
private B = new Object(); // added this just to make the point that
// the type of B is irrelavent
public int someMethod(){
synchronized(B){
....
// this code here cannot execute at the same time as ...
}
}
public int someOtherMethod(){
synchronized(B){
....
// ... this code down here
}
}
}

This, of course, includes multiple threads executing the same block, for example (sequence in time goes down):

Thread1 Thread2
======= =======
calls someMethod() .....
hits synchronized block calls someMethod()
lock on B is available hits synchronized block
acquires lock on B lock on B is not available
executes remainder of synchronized block ... waits ...
exits synchronized (releases lock on B) acquires lock on B
executes remainder of synch
exit synch block(release lock)
Chris Beckey
Ranch Hand

Joined: Jun 09, 2006
Posts: 116

Okay, so the formating of that did not work at all... try again in a different format (in this case the numbers indicate same time).

Thread1
=======
1.) calls someMethod()
2.) hits synchronized block
3.) lock on B is available
4.) acquires lock on B
5.) executes remainder of synchronized block
6.) exits synchronized (releases lock on B)
7.) ... goes on to other stuff

Thread 2
========
1.) ..... doing other stuff ...
2.) calls someMethod()
3.) hits synchronized block
4.) lock on B is not available
5.) ... waits ...
6.) acquires lock on B
7.) executes remainder of synch
8.) exit synch block(release lock)
Zarko Bucic
Greenhorn

Joined: May 25, 2006
Posts: 3
Chris, thank you very much for yours great efforts to explain me this stuff, but I still have one doubt about everything:

Thread1
=======
1.) calls someMethod()
2.) hits synchronized block
3.) lock on B is available
4.) acquires lock on B

In this moment between steps 4 and 6

5.) executes remainder of synchronized block

is the monitor of object A, available to other threads,i.e
If object of class A, have a method:
public synchronized void doStuffInA(){},
can Thread2,IN THIS MOMENT, execute method doStuffInA(),
and in same time acquire lock(monitor) on object A???


6.) exits synchronized (releases lock on B)
7.) ... goes on to other stuff

[ August 15, 2006: Message edited by: Zarko Bucic ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Synchronized blocks