I am a bit confused about the need and usage of the yield method. First of all, if we have two threads of different priority in a runnable state, does JVM give the equal opportunity to both threads to execute in a round-robin fashion.OR a high-priority thread will be given priority? Edit:- Assume OS is windows XP.
Now let’s come to the yield method of thread class.
The yield method Causes the currently executing thread object to temporarily pause and allow other threads(of higher priority or same priority) to execute.
At one of the articles is given at http://oreilly.com/catalog/expjava/excerpt/index.html, it is given at If at any time, a thread of a higher priority than the current thread becomes runnable, it preempts the lower priority thread and begins executing which is what yield is also doing/
So looks like from the above statements yield is automatically taken care of by JVM. Not sure what yield method is providing extra here?
Thread.yield() is essential if you have green threads, however almost every JDK since version 1.1 has used native threads.
Thread priority rarely matters in most applications.
If you have plenty free CPU, every thread which can run will run. The OS has no reason not to run a low priority thread or process when it has free resources.
If your system is close to 100% of CPU on every core, the OS has to make a choice as to how much time each thread or process gets on the CPU and it might give favour to high priority threads over lower priority threads, (many OSes ignore the hint) however other factors are likely to matter as well.
However this priority only extends to raw CPU. It doesn’t mean a higher priority thread get more CPU cache, main memory, memory bandwidth, file cache, disk IO or network IO. If any of these resource are in competition, they are all equal.