Giriş
Açıklaması şöyle.
Thread.sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system.Açıklaması şöyle.
Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers. The thread does not lose ownership of any monitors.
sleep metodu - long millis
Şöyle yaparız.Thread.sleep (1000);
Şu iki kod aynı işi görür.Thread.sleep (intervalInMills);
TimeUnit.MILLISECONDS.sleep (intervalInMills);
sleep(0)Örnek
Şöyle yaparız
int i = 0;
while (i<10_000_000) {
// business logic
//prevent long gc
if (i % 3000 == 0) {
try {
Thread.sleep(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Buradaki amaç şu değil
.. Thread.sleep(0)is not used to actively give up the CPU time slice,
Amaç şu
GC threads are known to have low priority, so Thread.sleep(0) is used to help GC threads try to compete for CPU time slice
Daha detaylı bir açıklama şöyle. GC işlemi rastgele bir yerde başlamaz. Sadece Safepoint noktalarda başlar. Döngüler Safepoint noktası değildir. Ancak Thread.sleep() bir Safepoint noktasıdır
Taking the HotSpot virtual machine as an example, the JVM does not pause at any position in the code instruction stream to start garbage collection, but forces that the execution must reach a safepoint before pausing. In other words, until the safepoint is reached, the JVM will not stop the world for GC.The JVM will set safepoints on some loop jumps and method calls. However, in order to avoid the heavy burden of too many safepoints, the HotSpot virtual machine also has an optimization measure for loops. If the number of cycles is small, the execution time should not be too long. Therefore, loops that use int or smaller data types as index values will not be placed with safepoint by default. This kind of loop is called a countable loop. Correspondingly, a loop that uses long or a larger range of data types as index values is called an uncounted loop and will be placed at a safepoint.However, we happen to have a countable loop here, so our code will not be placed at a safepoint. Therefore, the GC thread must wait until the thread finishes executing and can execute until the nearest safepoint. But if you use Thread.sleep(0), you can place a security point in the code.
Açıklamanın devamı şöyle. Yani 10 milyon defa dönen bir döngüde arada bir Thread.sleep() kullanarak Safepoint noktası oluşturur ve arada bir GC olmasını sağlar. Böylece döngüden çıkınca uzun bir GC olmasının önüne geçilir.
Thread.sleep(0) is not a useless code. The sleep method can be used to place a safepoint in the java code. It can trigger the GC in a long loop in advance to prevent the GC thread from waiting for a long time, thus avoiding the goal of lengthening the GC time. The people who wrote this code are too strong. It’s really awesome.
sleep metodu - long millis, int nanos
Java 21 için açıklaması şöyle
Thread.sleep(long millis, int nanos) can now perform sub-millisecond sleeps on POSIX platforms. Before, non-zero arguments for nanos were rounded up to a full millisecond before.Be aware that the actual precision still depends on the underlying system!
Hiç yorum yok:
Yorum Gönder