Yield Method Purpose to allow other high priority threads?
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 t开发者_开发百科emporarily 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.
How Thread.yield works is implementation specific. Effective Java 2nd Edition has some nice insight to this and it's stated that "Any program that relies on the thread scheduler for correctness or performance is likely to be nonportable."
Thread.yield might even be a no op in some implementations.
Thread.yield()
is a way to tell the JVM that the current thread has paused its execution and the JVM can allow other threads to use the CPU and other resources.
Thread.yield()
can be used in any thread instance regardless of the priority of the thread. Yielding a highest priority thread causes the JVM to allow the next thread in runnable state to use the CPU and other required resources, same in case of lower priority thread instance.
Lookout for this line "a Thread can also give up its time voluntarily with the yield() call" in explanation of Yield in the link provided by you.
First of all my question is that if we have two thread of different priority in runnablestate does jvm give the equal opportunity to both threads to execute in a round robin fashion .OR high priority thread will be given priority?
Neither, thread scheduling is handled by the os.
Thread priorities are not very intuitive in java. On linux, thread priorities set in the jvm have no effect by default.
精彩评论