yield()表示当前线程(running)愿意放弃cpu,回到runnable状态,但不保证别的线程会得到cpu jvm在运行时有可能会忽略yield() 即使执行了yield(),当前线程变成可运行状态,但这时它还是会去和别的线程抢cpu 这和优先级没有关系,但优先级相差越大,高优先级的线程越有可能得到cpu可以看一下这个http://freish.iteye.com/blog/1477715 java7的文档注释: /** * A hint to the scheduler that the current thread is willing to yield * its current use of a processor. The scheduler is free to ignore this * hint. * * <p> Yield is a heuristic attempt to improve relative progression * between threads that would otherwise over-utilise a CPU. Its use * should be combined with detailed profiling and benching to * ensure that it actually has the desired effect. * * <p> It is rarely appropriate to use this method. It may be useful * for debugging or testing purposes, where it may help to reproduce * bugs due to race conditions. It may also be useful when designing * concurrency control constructs such as the ones in the * {@link java.util.concurrent.locks} package. */ public static native void yield();
jvm在运行时有可能会忽略yield()
即使执行了yield(),当前线程变成可运行状态,但这时它还是会去和别的线程抢cpu
这和优先级没有关系,但优先级相差越大,高优先级的线程越有可能得到cpu可以看一下这个http://freish.iteye.com/blog/1477715
java7的文档注释: /**
* A hint to the scheduler that the current thread is willing to yield
* its current use of a processor. The scheduler is free to ignore this
* hint.
*
* <p> Yield is a heuristic attempt to improve relative progression
* between threads that would otherwise over-utilise a CPU. Its use
* should be combined with detailed profiling and benching to
* ensure that it actually has the desired effect.
*
* <p> It is rarely appropriate to use this method. It may be useful
* for debugging or testing purposes, where it may help to reproduce
* bugs due to race conditions. It may also be useful when designing
* concurrency control constructs such as the ones in the
* {@link java.util.concurrent.locks} package.
*/
public static native void yield();