while (server == null && count++ < 10) { // 省略。。 if (server == null) {/* Transient. */ Thread.yield(); continue; } // 省略。。 }
看代码应该是 9 年期,难道只是出让一次机会给别的线程执行?
![]() | 1 kkkkkrua 2021-09-18 17:25:45 +08:00 意思就是 sleep 的优雅版 |
2 carrotrollroll 2021-09-19 13:51:35 +08:00 这不是每次循环都出让一次嘛? |
3 xxxrubyxxx 2021-09-19 19:44:31 +08:00 让出线程拿到的时间片,让其他线程去竞争,Thread.sleep()会让线程阻塞在哪里,浪费 cpu 资源 |
![]() | 4 huang119412 2021-09-22 14:00:46 +08:00 看注释: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 |