File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronized block

 
Ram N Sharma
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ram N Sharma
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ram N Sharma
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic