WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Java Concurrency · Basic · question 11 of 100

How do the wait() and sleep() methods differ in Java?

📕 Buy this interview preparation book: 100 Java Concurrency questions & answers — PDF + EPUB for $5

Both the wait() and sleep() methods are used to pause the execution of a thread in Java, but they differ in their purpose and behavior.

The wait() method is used to pause the execution of a thread until another thread notifies it to wake up. The wait() method is typically used in multi-threaded applications where one thread is waiting for another thread to complete a specific task or release a resource. The wait() method is called on an object, and the thread that calls the method releases the lock on the object until it is notified to wake up.

Here’s an example of using the wait() method:

public class SharedResource {
    public synchronized void waitForSignal() throws InterruptedException {
        wait();
    }
    
    public synchronized void sendSignal() {
        notify();
    }
}

In this example, we define a class called SharedResource that contains a waitForSignal() method and a sendSignal() method. The waitForSignal() method is synchronized and calls the wait() method, causing the thread to wait until it is notified to wake up. The sendSignal() method is synchronized and calls the notify() method, notifying any waiting threads to wake up and continue executing.

The sleep() method, on the other hand, is used to pause the execution of a thread for a specific amount of time, regardless of whether or not another thread is ready to execute. The sleep() method is typically used in situations where a thread needs to wait for a specific amount of time before continuing execution. The sleep() method is called on a thread and does not release any locks on objects.

Here’s an example of using the sleep() method:

public class Main {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("Before sleep");
        Thread.sleep(1000);
        System.out.println("After sleep");
    }
}

In this example, we define a main() method that calls the sleep() method on the current thread, causing it to pause for one second before continuing execution.

In summary, the wait() and sleep() methods differ in their purpose and behavior. The wait() method is used to pause the execution of a thread until another thread notifies it to wake up, while the sleep() method is used to pause the execution of a thread for a specific amount of time, regardless of whether or not another thread is ready to execute.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Java Concurrency interview — then scores it.
📞 Practice Java Concurrency — free 15 min
📕 Buy this interview preparation book: 100 Java Concurrency questions & answers — PDF + EPUB for $5

All 100 Java Concurrency questions · All topics