• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

to yield() or not to yield()?

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)?
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ... ...
 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ... ...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic