Java Thread using a real-time timer in the system or does it have its own dedicated timer?

Does anyone know which timer uses the Thread.sleep(1000) method? Does Thread use a real-time timer, or does it have its own dedicated timer?

Thanks in advance for your answers.

+4
source share
3 answers

The Java language specification sends details of the semantics of this method to the base system. So the behavior will depend on the JVM implementation that you use rt.jar, and presumably also what OS and hardware the application is running.

This is all that is said about the method in JLS (Chapter 17: Threads and Locks):

17.9 Sleep and exit

Thread.sleep causes the current executable thread to sleep (temporarily stop execution) for the specified duration, taking into account the accuracy and precision of system timers and schedulers . The thread does not lose ownership of any monitors, and the resumption of execution will depend on the planning and availability of the processors on which the thread will run.

Neither sleep during the zero time period, nor the exit operation have observed effects.

It is important to note that neither Thread.sleep nor Thread.yield have synchronization semantics. In particular, the compiler does not need to hide the entries cached in the registers in shared memory before calling Thread.sleep or Thread.yield, and the compiler does not need to reload the values ​​cached in the registers after calling Thread.sleep or Thread.yield.

+5
source

Thread.sleep(long) is a native method. How this works depends on the OS and JVM implementation.

You can see its own power in jvm.cpp [openjdk.java.net] :

 JVM_ENTRY(void, JVM_Sleep(JNIEnv* env, jclass threadClass, jlong millis)) JVMWrapper("JVM_Sleep"); if (millis < 0) { THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative"); } if (Thread::is_interrupted (THREAD, true) && !HAS_PENDING_EXCEPTION) { THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted"); } // Save current thread state and restore it at the end of this block. // And set new thread state to SLEEPING. JavaThreadSleepState jtss(thread); HS_DTRACE_PROBE1(hotspot, thread__sleep__begin, millis); if (millis == 0) { // When ConvertSleepToYield is on, this matches the classic VM implementation of // JVM_Sleep. Critical for similar threading behaviour (Win32) // It appears that in certain GUI contexts, it may be beneficial to do a short sleep // for SOLARIS if (ConvertSleepToYield) { os::yield(); } else { ThreadState old_state = thread->osthread()->get_state(); thread->osthread()->set_state(SLEEPING); os::sleep(thread, MinSleepInterval, false); thread->osthread()->set_state(old_state); } } else { ThreadState old_state = thread->osthread()->get_state(); thread->osthread()->set_state(SLEEPING); if (os::sleep(thread, millis, true) == OS_INTRPT) { // An asynchronous exception (eg, ThreadDeathException) could have been thrown on // us while we were sleeping. We do not overwrite those. if (!HAS_PENDING_EXCEPTION) { HS_DTRACE_PROBE1(hotspot, thread__sleep__end,1); // TODO-FIXME: THROW_MSG returns which means we will not call set_state() // to properly restore the thread state. That likely wrong. THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted"); } } thread->osthread()->set_state(old_state); } HS_DTRACE_PROBE1(hotspot, thread__sleep__end,0); JVM_END 

In the above code, it calls the sleep operating system.

+4
source

From javadoc

It makes the current executable thread sleep (temporarily stop execution) for the specified number of milliseconds, taking into account the accuracy and accuracy of system timers and schedulers . The thread does not lose ownership of any monitors.

+2
source

Source: https://habr.com/ru/post/1336435/


All Articles