Sleep vs Wait

Sleep vs Wait

In Java, sleep and wait are both used to pause the execution of a thread, but they serve different purposes and have different behaviors:

Thread.sleep(milliseconds)

  • Purpose: Pauses the current thread for a specified number of milliseconds.
  • Usage: Thread.sleep(1000); (Pauses for 1 second)
  • Synchronization: It does not release the lock on any synchronized object.
  • Waking Up: The thread resumes automatically after the specified time has passed or if it’s interrupted.
  • Static Method: Belongs to the Thread class, so it directly affects the current thread.

Object.wait(milliseconds)

  • Purpose: Makes the current thread wait until another thread calls notify() or notifyAll() on the same object.
  • Usage: Must be called inside a synchronized block/method. For example:

synchronized(lock) {
    lock.wait();
}        

  • Synchronization: It releases the lock on the object it is waiting on, allowing other threads to access the synchronized block or method.
  • Waking Up: The thread resumes when notify() or notifyAll() is called on the same object, or if the optional timeout has passed.
  • Non-static Method: Belongs to the Object class, so it’s invoked on a specific object.

sleep is typically used to introduce delays, while wait is essential for thread coordination in scenarios where threads need to communicate and wait for specific conditions.

Doctor Lloyd Munyai

Full Stack Mobile Developer | Flutter Developer | Android Native Developer

4 周
回复

要查看或添加评论,请登录

Mothusi Molorane的更多文章

  • The for each loop

    The for each loop

    What is a forEach loop? The syntaxic sugar to iterate over an iterable, internally the compiler creates an iterator for…

  • Deciphering Spring Boot magic

    Deciphering Spring Boot magic

    What is spring auto configuration? First, this question seems to be easy because as you know, most developers use…

  • Spring Dependency Injection

    Spring Dependency Injection

    Dependency Injection Is an action of supplying dependencies to a dependant bean. Since most of my audience are familiar…

  • MicroService All In One

    MicroService All In One

    Good morning to my LinkedIn friend. Today, I want to share my presentation slides again.

  • Spring Authorisation Server

    Spring Authorisation Server

    SPRING SECURITY “Spring Security is a framework that provides authentication, authorization, and protection against…

  • Spring Data Series 2

    Spring Data Series 2

    To my LinkedIn audience, happy Tuesday morning. Remember the list of topics we are looking at in our series.

  • JPA and Spring Data

    JPA and Spring Data

    I have already published several articles on spring data and I will be building from them. If you have not read the…

  • I had an interview with a developer

    I had an interview with a developer

    Mothusi: Hello guys, today I have a Java interview with JavaSpace (aka JS) the name of my YouTube channel. Please help…

    3 条评论
  • Answer to puzzle 3

    Answer to puzzle 3

    Remember last week I posted this code snippet below. This code seem like it will loop from 123456789 to 0 however it's…

  • Java Puzzle 3

    Java Puzzle 3

    Before I go for weekend, I want to leave my LinkedIn friends with a fun Java code snippet. This time I am gonna do…

社区洞察

其他会员也浏览了