【问题标题】:Is java Thread using Real time timer in system or it has it's own dedicated timer?java线程是在系统中使用实时计时器还是有自己的专用计时器?
【发布时间】:2011-01-21 09:45:07
【问题描述】:

有人知道Thread.sleep(1000) 方法使用什么计时器吗? Thread 是使用实时系统计时器还是有自己的专用计时器?

提前感谢您的回答。

【问题讨论】:

    标签: java multithreading java-me timer


    【解决方案1】:

    Java 语言规范将此方法的语义细节推迟到底层系统。因此,行为将取决于哪个 JVM 实现、您使用的 rt.jar 以及应用程序运行在哪个操作系统和硬件上。

    这就是JLS(第17章:线程和锁)中关于方法的全部内容:

    17.9 睡眠和屈服

    Thread.sleep 使当前执行的线程休眠(暂时停止执行)指定的持续时间,取决于系统计时器和调度程序的精度和准确性。线程不会失去任何监视器的所有权,并且恢复执行将取决于调度和执行线程的处理器的可用性。

    零时间的休眠和让出操作都不需要有可观察的效果。

    需要注意的是,Thread.sleep 和 Thread.yield 都没有任何同步语义。特别是,编译器不必在调用 Thread.sleep 或 Thread.yield 之前将缓存在寄存器中的写入刷新到共享内存,编译器也不必在调用 Thread.sleep 或 Thread 之后重新加载缓存在寄存器中的值.yield。

    【讨论】:

      【解决方案2】:

      Thread.sleep(long) 是本机方法。它的工作方式取决于操作系统和 JVM 实现。

      可以在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 (e.g., 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's 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
      

      在上面的代码中,它调用了操作系统的sleep函数。

      【讨论】:

        【解决方案3】:

        来自javadoc

        使当前正在执行的线程休眠(暂时停止执行)指定的毫秒数,这取决于系统计时器和调度程序的精度和准确性。该线程不会失去任何监视器的所有权。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-09-21
          • 1970-01-01
          • 2010-10-11
          • 1970-01-01
          • 2021-03-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多