Thread.sleep() implementation

后端 未结 4 1997
北恋
北恋 2021-01-30 14:06

Today I had an interview on which I asked candidate quite usual and basic question about the difference between Thread.sleep() and Object.wait(). I exp

4条回答
  •  走了就别回头了
    2021-01-30 14:44

    Do you know any other reasons for not using timed wait in Thread.sleep() implementation?

    Because the native thread libraries provide a perfectly good sleep function: http://www.gnu.org/software/libc/manual/html_node/Sleeping.html

    To understand why native threads are important, start at http://java.sun.com/docs/hotspot/threads/threads.html

    Version 1.1 is based on green threads and won't be covered here. Green threads are simulated threads within the VM and were used prior to going to a native OS threading model in 1.2 and beyond. Green threads may have had an advantage on Linux at one point (since you don't have to spawn a process for each native thread), but VM technology has advanced significantly since version 1.1 and any benefit green threads had in the past is erased by the performance increases over the years.

提交回复
热议问题