Muh*_*edy 9 java multithreading thread-sleep
以下代码:
long msBefore = System.currentTimeMillis();
//Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
try
{Thread.sleep(200);
} catch (InterruptedException e){}
System.out.println("Time: " + (System.currentTimeMillis() - msBefore));
Run Code Online (Sandbox Code Playgroud)
印刷品:
Time: 578
Time: 594
Time: 625
Time: 640
Time: 641
Time: 609
Time: 625
Time: 625
Time: 610
Time: 609
Time: 625
Time: 625
Time: 422
Time: 625
Time: 594
Time: 609
Time: 625
Time: 594
Time: 594
Time: 625
Run Code Online (Sandbox Code Playgroud)
问题出在哪里?
这里没有问题.来自javadoc:
受制于系统和调度程序的准确性.
通常,依赖于休眠间隔是不好的设计,因为它在不同的系统和JVM实现上可能是不同的.改为使用wait()和notify(),或者更好 - 使用java.util.concurrent包.