wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes synchronized block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "synchronized block" Watch "synchronized block" New topic
Author

synchronized block

Ram N Sharma
Greenhorn

Joined: Feb 19, 2005
Posts: 26
Is there any way possible for non-synchronized code within a method to execute while an object locked by a thread within a synchronized block is on wait?. i.e.

public void run()
{
synchronized(O)
{
do stuff
O.wait();
}

Non-synch code here....

}

If the answer is that "no the non-synch code will never execute unless the synch block completes" then why even have a synch block instead why not just synchronize the method? i.e. why even have the ability to synch one section of the method?

Thanks
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Non-synchronized code does not require an object (or class) lock to execute. So if a blocked thread has the lock, this will not prevent non-synchronized code from executing in another thread.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ram N Sharma
Greenhorn

Joined: Feb 19, 2005
Posts: 26
marc
Thanks for your reply. I am still not clear. I tried setting up a simple test in which I tried running 2 instances of a class in 2 diff threads. Passed a static object reference to the constructor of both. Then coded a sleep(xxx) (so it takes the locks with it) . And also inserted a few lines of non-synch code within the method .

But the results are still the same i.e. the threads wait for the lock to be released before moving on to the non-synch methods. What am i missing?
Thanks
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Ram N Sharma
Greenhorn

Joined: Feb 19, 2005
Posts: 26
Marc

Referring back to my first posting... The non-synch code is part of the same method in which the synch block is located. Not as a separate method. This is what is puzzling. for example

public void run()
{
synchronized(O)
{
do stuff
Thread.sleep(9999);
}

Non-synch code here....>>>>( This will only execute after the lock is released)

} // - End of method

Thanks
raghu babu
Ranch Hand

Joined: Jul 05, 2005
Posts: 60
To answer your original question, I don't think there is no way that non-synchronized code would get a chance to be executed while an object locked by a thread within a synchronized block is on wait (provided you are referring to the non-synchronized code that exists after the synchronization block).

When the thread is waiting to acquire the lock on an object (as the lock is already owned by another thread) it goes into 'blocked for lock acquisition' state, and wouldn't get a chance to run until it acquires the lock on the object it is waiting for.

This is my understanding, I'm sure someone would correct the above explanation, if I'm misguiding.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Ram N Sharma:
... Referring back to my first posting... The non-synch code is part of the same method in which the synch block is located. Not as a separate method...

I see. In order to get to the non-synchronized code, a thread would need to call that method and then acquire the lock simply to get past the synchronized block.

But consider this...

Thread #1 acquires the lock, executes the synchronized code, releases the lock, and then -- before Thread #1 can move on to the non-synchronized code within the method -- the thread scheduler allows Thread #2 to run. Thread #2 acquires the lock, and then blocks. While Thread #2 is blocked (with the lock), the thread scheduler allows Thread #1 to run again, and Thread #1 executes the non-synchronized code even through another thread with the lock is blocked.

Isn't this possible?
raghu babu
Ranch Hand

Joined: Jul 05, 2005
Posts: 60
Marc, I absolutely agree with this scenario.

I have one more question, and thought would bring it here.

If a method is synchronized, and when a thread enters this synchronized
method (assuming the thread got hold of the object lock and method
doesn't have any statements that make the thread to go into blocked status), is it always true that once thread enters into this method
it is guaranteed to execute until the end of the method ATOMICALLY (i.e., would thread scheduler can choose another thread to run so whatever thread currently executing synchorinzed method won't get a chance to finish atomically). Pl. let me know if my question doesn't make sense.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by vneeth babu:
... If a method is synchronized ... is it always true that once thread enters into this method it is guaranteed to execute until the end of the method ATOMICALLY...

I don't think so. If my understanding of atomic operations is correct, then only operations on primitives (other than long or double on a 32-bit machine) or object references are "atomic."

Synchronizing methods does not prevent other threads from getting CPU time (that is, synchronized methods are not atomic). Instead, synchronization simply prevents other threads from obtaining the lock necessary to execute synchronized code.

Can someone verify my understanding?
Ram N Sharma
Greenhorn

Joined: Feb 19, 2005
Posts: 26
Thanks for the replies. I suppose what Marc mentioned in the last posting is possible.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: synchronized block
 
Similar Threads
A static synchronized method and a non-static synchronized method will not block each other, ever.
Synchronization
what wait() should be called from Synchrnoized block
wait method calls
Synchronizing Code