Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7192493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:57:08+00:00 2026-05-28T19:57:08+00:00

Today I had an interview on which I asked candidate quite usual and basic

  • 0

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 expected him to answer something like like this, but he said these methods basically are the same thing, and most likely Thread.sleep is using Object.wait() inside it, but sleep itself doesn’t require external lock. This is not exactly a correct answer, because in JDK 1.6 this method have following signature.

public static native void sleep(long millis) throws InterruptedException;

But my second thought was that it’s not that ridiculous. It’s possible to use timed wait to achieve the same effect. Take a look at the following code snippet:

public class Thread implements Runnable {
       private final Object sleepLock = new Object();

     // other implementation details are skipped

       public static void sleep(long millis) throws InterruptedException {
            synchronized (getCurrentThread().sleepLock){
                getCurrentThread().sleepLock.wait(millis);
            }
        }

In this case sleepLock is an object which is used particularly for the synchronization block inside sleep method. I assume that Sun/Oracle engineers are aware of Occam’s razor, so sleep has native implementation on purpose, so my question is why it uses native calls.

The only idea I came up with was an assumption that someone may find useful invocation like Thread.sleep(0). It make sense for scheduler management according to this article:

This has the special effect of clearing the current thread’s quantum and putting it to the end of the queue for its priority level. In other words, all runnable threads of the same priority (and those of greater priority) will get a chance to run before the yielded thread is next given CPU time.

So a synchronized block will give unnecessary overhead.

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:57:09+00:00Added an answer on May 28, 2026 at 7:57 pm

    One could easily say Occam’s Razor cuts the other way. The normal/expected implementation of the JVM underlying JDK is assumed to bind java ‘threads’ onto native threads most of the time, and putting a thread to sleep is a fundamental function of the underlying platform. Why reimplement it in java if thread code is going to be native anyway? The simplest solution is use the function that’s already there.

    Some other considerations:
    Uncontested synchronization is negligible in modern JVMs, but this wasn’t always so. It used to be a fairly “expensive” operation to acquire that object monitor.

    If you implement thread sleeping inside java code, and the way you implement it does not also bind to a native thread wait, the operating system has to keep scheduling that thread in order to run the code that checks if it’s time to wake up. As hashed out in the comments, this would obviously not be true for your example on a modern JVM, but it’s tough to say
    1) what may have been in place and expected at the time the Thread class was first specified that way.
    and
    2) If that assertion works for every platform one may have ever wanted to implement a JVM on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had an interview today and the person asked me this question: How do
I had an interview today which involved this very question and in order to
I had an interview today and was asked this question! code the MS Paint
This question Is a result of various questions I had today about cookies. As
Today I had an interview with a gentleman who asked me to determine how
I heard this today during interview for java developer. I had to list some
Today I had a discussion with a colleague about nested functions in Javascript: function
Today I had to fix some older VB.NET 1.0 code which is using threads.
I had an interview today and every thing was going very good, but then
Today we had a discussion at the office about UI elements in the required

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.