wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes to yield() or not to yield()? 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 "to yield() or not to yield()?" Watch "to yield() or not to yield()?" New topic
Author

to yield() or not to yield()?

Abhinav Yadav
Ranch Hand

Joined: Nov 08, 2009
Posts: 34
the Thread class defines the static method yield() whose function is to transit the thread from currently executing state to the Ready-to-run state so that other threads that were in ready-to-run states but were currently not executing get a chance to execute but at the same time it does not forces that thread to release the lock that it might have been holding, so what will be the use of yield() method when another thread wants to execute the same synchronized method (since the lock has not yet been released)?


No Dream Too Big
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
There are many (most) cases where object locking is not involved; when
the JVM is time-sharing between independent programs, for example.

Jim ... ...


BEE MBA PMP SCJP-6
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

abhinav yadav wrote:the Thread class defines the static method yield() whose function is to transit the thread from currently executing state to the Ready-to-run state so that other threads that were in ready-to-run states but were currently not executing get a chance to execute but at the same time it does not forces that thread to release the lock that it might have been holding, so what will be the use of yield() method when another thread wants to execute the same synchronized method (since the lock has not yet been released)?


good question.

The answer is that yield() is useful only outside of synchronized blocks. If you think you need to use yield, make a synchronized block inside the method instead of the method itself, and use the yield() outside of the synchronized block
Abhinav Yadav
Ranch Hand

Joined: Nov 08, 2009
Posts: 34

The answer is that yield() is useful only outside of synchronized blocks. If you think you need to use yield, make a synchronized block inside the method instead of the method itself, and use the yield() outside of the synchronized block


ok but since the threads hold lock onto the object of a class and not upon the methods ,so if the lock has not been released and the object is still in hold of another thread then there might be the condition that the lock is never released(or will it be).
And i think using synchronized block inside the method is as good as making the method synchronized.If not then please give an example it would be really easy to compare then.
Abhinav Yadav
Ranch Hand

Joined: Nov 08, 2009
Posts: 34
Jim Hoglund wrote:There are many (most) cases where object locking is not involved; when
the JVM is time-sharing between independent programs, for example.

Jim ... ...


i didn't understood your point sir.
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

Thread.yield() is a static method, so it causes the currently executing thread to give up control.

here is one example from the scimark benchmarking code:


it calls Thread.yield after each operation so that other threads can get a chance to run before it enters into the next potentially time-consuming operation. In this case, it gives the gui a chance to update.
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Abhinav : Let's take this one step at a time.
[With yield()] other threads ... get a chance to execute but at the same time it does not force
that thread to release the lock that it might have been holding. So what will be the use
of the yield() method when another thread wants to execute the same synchronized method,
since the lock has not yet been released?

1) As you said, yield() does not release an object lock. But there may not
be any lock to release. Remember that yield() can be used anywhere either
inside or outside of synchronized code.

2) Methods are not locked. It is objects that are locked. This means that in
a different thread, the same synchronized method can lock and operate on
a different object instance. And also in a different thread, non-synchronized
methods can operate on locked objects. This is why all access to a critical
object must be synchronized (locked) access.

3) So yield() allows the other threads to run. At some point one of them may
request the object lock held by the yielding thread, but this depends on the
software design and each thread's current state.

Jim ... ...
 
GeeCON Prague 2014
 
subject: to yield() or not to yield()?