当前位置:网站首页>Multithreading Foundation (XI) -- prevent CPU from occupying 100%

Multithreading Foundation (XI) -- prevent CPU from occupying 100%

2022-06-12 06:19:00 leo_ messi94

sleep Realization

Without using cpu To calculate , Don't let while(true) Idling waste cpu, You can use yield or sleep Come out cpu The right to use is given to other programs

while(true) {
    
	try {
    
		Thread.sleep(10);
	} catch (InterruptedException e) {
    
		e.printStackTrace();
	}
}
  • It can be used wait Or conditional variables to achieve similar effects
  • The difference is , The latter two need to be locked , And the corresponding wake-up operation is required , It is generally applicable to the scene to be synchronized
  • sleep It is suitable for scenarios without lock synchronization

wait Realization

原网站

版权声明
本文为[leo_ messi94]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010611072964.html