我的原则:先会用再说,内部慢慢来
Thread.yeild
一、 作用
- Thread.yeild 线程礼让,当前线程暂时不跑了,让其他线程先跑。类似于你去银行排队办事情,你跑到最后去重新拿个号重新排队。
二、 注意点
- yield 跟锁没关系,也就是跟synchronized没关系,也就是并不会释放锁。
三、 代码Demo
public class _05_01_YieldTest implements Runnable {
@Override
public void run(){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 3; i++) {
System.out.println(Thread.currentThread() + ": " + i);
Thread.yield();
}
}
public static void main(String[] args) throws Exception {
_05_01_YieldTest runn = new _05_01_YieldTest();
Thread t1 = new Thread(runn, "t1");
Thread t2 = new Thread(runn, "t2");
t1.start();
t2.start();
Thread.sleep(100);
for (int i = 0; i < 3; i++) {
System.out.println("Main " + Thread.currentThread() + ": " + i);
Thread.yield();
}
}
}
输出:
Thread[t1,5,main]: 0
Thread[t2,5,main]: 0
Main Thread[main,5,main]: 0
Thread[t1,5,main]: 1
Thread[t2,5,main]: 1
Main Thread[main,5,main]: 1
Thread[t2,5,main]: 2
Thread[t1,5,main]: 2
Main Thread[main,5,main]: 2
这个例子就是通过yield方法来实现三个线程的交替执行。
不过请注意:这种交替并不一定能得到保证,源码中也对这个问题进行说明:
四、 方法简述
/**
* 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.
意思是,给了调度器scheduler一个提示,我愿意让出当前的处理器processor给其他人,但是人家processor未必搭理你这个暗示。
*
* <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 benchmarking 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();
/*
这个例子就是通过yield方法来实现两个线程的交替执行。
不过请注意:这种交替并不一定能得到保证,源码中也对这个问题进行说明:
主要说明了三个问题:
调度器可能会忽略该方法。
使用的时候要仔细分析和测试,确保能达到预期的效果。
很少有场景要用到该方法,主要使用的地方是调试和测试。
*/
yield方法的作用是暂停当前线程,以便其他线程有机会执行,不过不能指定暂停的时间,
并且也不能保证当前线程马上停止。yield方法只是将Running状态转变为Runnable状态。
五、 独角戏
public class _05_02_YieldTest {
public static void main(String[] args) throws Exception {
for (int i = 0; i < 3; i++) {
System.out.println("Main " + Thread.currentThread() + ": " + i);
Thread.yield();
}
}
}
输出:
Main Thread[main,5,main]: 0
Main Thread[main,5,main]: 1
Main Thread[main,5,main]: 2
爱的魔力转圈圈,后面没其他人(Thread)了,只有你自己在排队。
为第六步做一个铺垫。
六、 加个锁 synchronized
public class _05_03_YieldTest implements Runnable {
@Override
public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (this){
for (int i = 0; i < 3; i++) {
System.out.println(Thread.currentThread() + ": " + i);
Thread.yield();
}
}
}
public static void main(String[] args) throws Exception{
_05_03_YieldTest runn = new _05_03_YieldTest();
Thread t1 = new Thread(runn, "t1");
Thread t2 = new Thread(runn, "t2");
t1.start();
t2.start();
Thread.sleep(100);
synchronized (runn){
for (int i = 0; i < 3; i++) {
System.out.println("Main " + Thread.currentThread() + ": " + i);
Thread.yield();
}
}
}
}
输出:
Thread[t1,5,main]: 0
Thread[t1,5,main]: 1
Thread[t1,5,main]: 2
Main Thread[main,5,main]: 0
Main Thread[main,5,main]: 1
Main Thread[main,5,main]: 2
Thread[t2,5,main]: 0
Thread[t2,5,main]: 1
Thread[t2,5,main]: 2
结论:
啥意思呢?就是说,虽然我已经 yield 妥协让步给其他人,但是我锁住了,你们现在也没法用,等我跑完你们再来吧。
七、优先级Priority问题
public class _05_04_YieldTest implements Runnable {
@Override
public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 3; i++) {
System.out.println(Thread.currentThread() + ": " + i);
Thread.yield();
}
}
public static void main(String[] args) throws Exception {
_05_04_YieldTest runn = new _05_04_YieldTest();
Thread t1 = new Thread(runn, "t1");
Thread t2 = new Thread(runn, "t2");
Thread t3 = new Thread(runn, "t3");
t1.setPriority(Thread.MAX_PRIORITY);
t3.setPriority(Thread.MIN_PRIORITY);
t1.start();
t2.start();
t3.start();
}
}
输出:
Thread[t3,1,main]: 0
Thread[t1,10,main]: 0
Thread[t2,5,main]: 0
Thread[t3,1,main]: 1
Thread[t1,10,main]: 1
Thread[t2,5,main]: 1
Thread[t3,1,main]: 2
Thread[t1,10,main]: 2
Thread[t2,5,main]: 2
你看优先级priority 最低的 t3 居然最先跑。
结论: priority 不起作用。
八、实战结论
- yield 能够让线程交替进行 (_05_01_YieldTest)
- yield 如果只有一个线程,那么会继续运行 ( _05_02_YieldTest)
- yield 方法和同步没关系,也就是和ObjectMonitor没关系,你硬上锁就是在唱独角戏 ( _05_03_YieldTest)
- 优先级priority 不生效。(_05_04_YieldTest)
九、JVM源码
十、 番外篇
上一章节:【线程】 Thread.sleep 与 Object.wait 的区别 (三)
下一章节:【线程】 Thread.sleep 与 Thread.yield 的区别 (五)