Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

to yield() or not to yield()?

 
Abhinav Yadav
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)?
 
Jim Hoglund
Ranch Hand
Posts: 525
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are many (most) cases where object locking is not involved; when
the JVM is time-sharing between independent programs, for example.

Jim ... ...
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 525
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ... ...
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic