
while (server == null && count++ < 10) { // 省略。。 if (server == null) { /* Tranient. */ Thread.yield(); continue; } // 省略。。 }
看代码应该是 9 年期,难道只是出让一次机会给别的线程执行?
1 kkkkkrua 2021 年 9 月 18 日 意思就是 sleep 的优雅版 |
2 carrotrollroll 2021 年 9 月 19 日 这不是每次循环都出让一次嘛? |
3 xxxrubyxxx 2021 年 9 月 19 日 让出线程拿到的时间片,让其他线程去竞争,Thread.sleep()会让线程阻塞在哪里,浪费 cpu 资源 |
4 huang119412 2021 年 9 月 22 日 看注释: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. 实际上这个可以极大增加 CPU 利用率,Disruptor 高性能就是靠 CAS + yield |