• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

yield() method

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
K&B book states that the yield() method may cause a running thread to back out if there are runnable threads of the SAME priority.
In one of the mock tests (don't remember which one), it states that the yield() method may cause a running thread to back out if there are runnable threads of the SAME OR HIGHER priority.

Which one is correct?

Thanks.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a new Thread with HIGHER priority enters the Runnable State....the thread scheduler will automatically pre-empt the running thread and put in the Higher priority thread in the Running state. If you happen to call yield()...at a moment when a HIgher priority thread enters the Runnable thread....the currently running thread could back out due to either of the reasons

Calling yield() or
Thread Scheduler

However tht precise timing of calling yield() is rare...so more often than not, yield() is used to back off a thread to offer CPU time to same priority threads
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yield() will yield to a thread of higher priority as well of course.

Be careful when thinking in terms of pre-empting... JVMs do not necessarily use pre-emptive scheduling. In such cases some well-placed yield()s may be absolutely necessary to get the level of concurrency you need.

- Peter
 
Attractive, successful people love this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic