和有什么区别 wait()sleep() 在线程中?

我的理解是 wait()-ing 线程仍处于运行模式并使用 CPU 周期,但 sleep()-ing 不消耗任何 CPU 周期,对吗?

为什么我们有 两个都 wait()sleep(): :它们的实施在较低级别有何不同?

有帮助吗?

解决方案

wait 可以<!>“唤醒<!>”;另一个线程调用 notify ,而 sleep 不能。此外,synchronized(和mon)必须在监视器对象的块notifyAll中发生,而Object不会:

Object mon = ...;
synchronized (mon) {
    mon.wait();
} 

此时,当前正在执行的线程等待并释放监视器。另一个线程可以做

synchronized (mon) { mon.notify(); }

(在相同的Thread对象上)和第一个线程(假设它是在监视器上等待的唯一线程)将被唤醒。

你也可以打电话给 <=> 如果监视器上有多个线程正在等待<!>#8211;这将唤醒所有这些。但是,只有一个线程能够获取监视器(请记住<=>位于<=>块中)并继续执行<!>#8211;其他人将被阻止,直到他们可以获得监视器的锁定。

另一点是你在。 html“rel =”noreferrer“> <=> 本身(即你在对象的监视器上等待),而你在 /javase/11/docs/api/java.base/java/lang/Thread.html"rel =“noreferrer”> <=>

另一点是你可以从<=>获得虚假唤醒(即等待恢复的线程没有明显原因)。您应该始终<=>在某种情况下旋转,如下所示:

synchronized {
    while (!condition) { mon.wait(); }
}

其他提示

尚未提及的一个关键区别是,在休眠线程时释放它所持有的锁,而等待会释放对wait()被调用的对象的锁定。

synchronized(LOCK) {
    Thread.sleep(1000); // LOCK is held
}


synchronized(LOCK) {
    LOCK.wait(); // LOCK is not held
}

在这里,我列出了wait()sleep()方法之间的一些重要区别。点击 PS: 同时点击链接查看图书馆代码(内部工作,只是为了更好的理解而玩一下)。

wait()

  1. Object方法释放锁。
  2. public final void wait() throws InterruptedException { //...} notify() class。
  3. 的方法
  4. notifyAll()是非静态方法 - IllegalMonitorStateException
  5. java.lang.Thread应通过public static void sleep(long millis, int nanos) throws InterruptedException { //... }或<=>方法通知。
  6. <=>需要从循环中调用方法以处理误报。

  7. 必须从同步上下文(即同步方法或块)调用
  8. <=>方法,否则将抛出<=>

  9. sleep()

    1. <=>方法不释放锁。
    2. <=>是<=> class。
    3. 的方法
    4. <=>是静态方法 - <=>
    5. 在指定的时间后,<=>完成。
    6. <=>最好不要从循环中调用(即见下面的代码)。
    7. <=>可以从任何地方调用。没有具体要求。
    8. 参考:等待和睡眠之间的区别

      调用等待和睡眠方法的代码段

      synchronized(monitor){
          while(condition == true){ 
              monitor.wait()  //releases monitor lock
          }
      
          Thread.sleep(100); //puts current thread on Sleep    
      }
      

在处理 wait 和 sleep 后,我总结出一些不同的要点,首先看一下使用 wait() 和 sleep() 的示例:

示例1: :使用 等待() 和 睡觉():

synchronized(HandObject) {
    while(isHandFree() == false) {
        /* Hand is still busy on happy coding or something else, please wait */
        HandObject.wait();
    }
}

/* Get lock ^^, It is my turn, take a cup beer now */
while (beerIsAvailable() == false) {
    /* Beer is still coming, not available, Hand still hold glass to get beer,
       don't release hand to perform other task */
    Thread.sleep(5000);
}

/* Enjoy my beer now ^^ */
drinkBeers();

/* I have drink enough, now hand can continue with other task: continue coding */
setHandFreeState(true);
synchronized(HandObject) {
    HandObject.notifyAll();
}

让我们澄清一些要点:

  1. 致电:
    • 等待():调用当前持有 HandObject 对象的线程
    • 睡觉():调用线程执行任务获取啤酒(是类方法,因此会影响当前正在运行的线程)
  2. 同步:
    • 等待():当同步多线程访问同一个对象(HandObject)时(当多个线程之间需要通信时(线程执行编码,线程执行获取啤酒)访问同一个对象 HandObject )
    • 睡觉():当等待条件继续执行时(等待啤酒可用)
  3. 保持锁定:
    • 等待():释放锁让其他对象有机会执行(HandObject是空闲的,你可以做其他工作)
    • 睡觉():保持锁定至少 t 次(或直到中断)(我的工作仍未完成,我继续保持锁定并等待某些条件继续)
  4. 唤醒条件:
    • 等待():直到从对象调用notify()、notifyAll()
    • 睡觉():至少直到时间到期或呼叫中断
  5. 最后一点是 使用时 作为 埃斯塔尼 表明:

通常,您使用Sleep()进行时间合成,并使用wait()进行多线程同步。

如果我错了,请纠正我。

wait() 和 sleep() 之间的区别

  • 根本的区别在于 wait() 来自 Objectsleep() 是一个静态方法 Thread.

  • 主要的区别在于 wait() 释放锁,同时 sleep() 等待时不释放任何锁。

  • wait() 用于线程间通信,而 sleep() 通常用于引入执行暂停。

  • wait() 应该从内部同步调用,否则我们会得到一个 IllegalMonitorStateException, , 尽管 sleep() 可以在任何地方调用。

  • 再次启动一个线程 wait(), ,你必须打电话 notify() 或者 notifyAll(). 。至于 sleep(), 线程在指定的时间间隔后启动。

相似之处

  • 两者都使当前线程进入 不可运行 状态。
  • 两者都是 本国的 方法。

这是一个非常简单的问题,因为这两种方法都有完全不同的用法。

主要区别在于等待释放锁定或监视器,而等待时睡眠不释放任何锁定或监视器。等待用于线程间通信,而睡眠用于在执行时引入暂停。

这只是一个清晰而基本的解释,如果你想要更多,那么继续阅读。

如果wait()方法线程进入等待状态,并且在我们调用notify()方法之前它不会自动返回(或notifyAll()如果你有多个线程处于等待状态并且你想要唤醒所有这些线程)。并且您需要同步或对象锁或类锁来访问sleep()ThreadObject方法。还有一件事,<=>方法用于线程间通信,因为如果一个线程进入等待状态,你需要另一个线程来唤醒该线程。

但是在<=>的情况下,这是一种方法,用于将过程保持几秒钟或您想要的时间。因为您不需要激发任何<=>或<=>方法来获取该线程。或者您不需要任何其他线程来回调该线程。就像你想要的东西应该在几秒钟之后发生,比如在用户轮到你的游戏之后,你希望用户等到计算机播放然后你可以提到<=>方法。

在访谈中经常会提到一个更重要的区别:<=>属于<=>类,<=>属于<=>类。

这些是<=>和<=>之间的所有差异。

两种方法之间存在相似之处:它们都是检查语句,因此您需要尝试catch或throws来访问这些方法。

我希望这会对你有所帮助。

来源: http://www.jguru.com/faq/view。 JSP?EID = 47127

  

Thread.sleep() 将当前线程发送到 <!> quot; Not Runnable <!> quot; 状态   一段时间。该线程保留了它所获得的监视器    - 即如果线程当前处于同步块或方法中,则其他线程无法进入该块或方法。如果另一个线程调用 t.interrupt() 它会唤醒沉睡的线程。

     

请注意,sleep是一种静态方法,这意味着它始终会受到影响   当前线程(正在执行sleep方法的线程)。一个   常见的错误是调用t.sleep(),其中t是一个不同的线程;   即便如此,它是当前正在休眠的线程,而不是t线程。

     

t.suspend() 已弃用。使用它可以停止其他线程   比当前的线程。挂起的线程会保留所有监视器和   由于这种状态不可中断,因此容易出现死锁。

     

object.wait() 将当前线程发送到 <!> quot; Not Runnable <!> quot; 状态,   喜欢sleep(),但有一点扭曲。在对象上调用Wait,而不是   线;我们称这个对象为<!> quot; lock对象。<!> quot;在lock.wait()之前   调用时,当前线程必须在锁定对象上同步; wait()   然后释放此锁,并将线程添加到<!>“;等待列表<!>”;   与锁相关联。以后,另一个线程可以同步   相同的锁定对象并调用lock.notify()。这唤醒了原作,   等待线程。基本上,notify() / interrupt()就像   <=> / <=>,只有活动线程不需要直接   指向休眠线程的指针,但仅指向共享锁对象。

等待和睡眠是两件不同的事情:

  • sleep() 线程在指定的时间内停止工作。
  • wait() 线程停止工作,直到通知正在等待的对象(通常是由其他线程通知)。

sleepThread的方法,waitObject的方法,所以wait/notify是一种在Java中同步共享数据的技术(使用监视器),但<=>是一种简单的线程暂停方法。

睡觉() 是一种用于将进程保持几秒钟或您想要的时间的方法,但如果使用 wait() 方法,线程将进入等待状态,直到我们调用 notification() 或 notifyAll() 为止,它不会自动返回。

主要区别 就是它 等待() 释放锁或监视器,而 sleep() 在等待时不会释放任何锁或监视器。通常,等待用于线程间通信,而睡眠用于引入执行暂停。

线程.sleep() 将当前线程置于“不可运行”状态一段时间。线程保留它已获取的监视器 - 即如果线程当前位于同步块或方法中,则其他线程无法进入此块或方法。如果另一个线程调用 t.interrupt() 它将唤醒睡眠线程。请注意,sleep 是一种静态方法,这意味着它始终影响当前线程(正在执行 sleep 方法的线程)。一个常见的错误是调用 t.sleep(),其中 t 是不同的线程;即便如此,当前线程将处于休眠状态,而不是 t 线程。

对象.wait() 将当前线程发送到“不可运行”状态,类似于 sleep(),但有所不同。Wait 是在对象上调用的,而不是在线程上调用的;我们称此对象为“锁定对象”。在调用lock.wait()之前,当前线必须同步在锁定对象上。wait() 然后释放该锁,并将该线程添加到与该锁关联的“等待列表”中。稍后,另一个线程可以在同一个锁对象上进行同步并调用lock.notify()。这会唤醒原来的等待线程。基本上,wait()/notify()就像sleep()/interrupt(),只是活动线程不需要指向休眠线程的直接指针,而只需要指向共享锁对象。

synchronized(LOCK) {   
   Thread.sleep(1000); // LOCK is held
}

synchronized(LOCK) {   
   LOCK.wait(); // LOCK is not held
}

让我们对以上几点进行分类:

Call on:

  • 等待(): 调用一个对象;当前线程必须在锁对象上同步。
  • 睡觉(): 调用线程;始终是当前正在执行的线程。

Synchronized:

  • 等待(): 当同步时,多个线程一一访问同一个对象。
  • 睡觉(): 当同步时,多个线程等待睡眠线程的睡眠结束。

Hold lock:

  • 等待(): 释放锁,让其他对象有机会执行。
  • 睡觉(): 如果指定超时或有人中断,则保持锁定至少 t 次。

Wake-up condition:

  • 等待(): 直到从对象调用notify()、notifyAll()
  • 睡觉(): 至少直到时间到期或调用interrupt()。

Usage:

  • 睡觉(): 用于时间同步;
  • 等待(): 用于多线程同步。

参考:差异 sleepwait

waitsleep 方法有很大不同:

  • sleep 没有办法“醒来”,
  • 然而 wait 有一种在等待期间“唤醒”的方法,由另一个线程调用 notify 或者 notifyAll.

想想看,这些名字在这方面是令人困惑的;然而 sleep 是一个标准名称并且 wait 就像 WaitForSingleObject 或者 WaitForMultipleObjects 在Win API 中。

简单来说,等待是等待直到某个其他线程调用你,而sleep是<!>“;不执行下一个语句<!>”;在一段特定的时间内。

此外,sleep是Thread类中的静态方法,它在线程上运行,而wait()在Object类中并在对象上调用。

另一点,当你在某个对象上调用wait时,所涉及的线程会同步对象然后等待。 :)

从这篇文章: http://javaconceptoftheday.com /差之间-等待和睡眠的方法合的java /

wait()方法。

1)调用wait()方法的线程释放它所持有的锁。

2)在其他线程调用同一个锁上的notify()或notifyAll()方法后,线程重新获得锁定。

3)必须在synchronized块中调用wait()方法。

4)始终在对象上调用wait()方法。

5)通过调用notify()或notifyAll()方法,其他线程可以唤醒等待线程。

6)要调用wait()方法,线程必须有对象锁。

sleep()方法

1)调用sleep()方法的线程不会<!>#8217;释放它所持有的锁。

2)sleep()方法可以在synchronized块内外调用。

3)总是在线程上调用sleep()方法。

4)其他线程无法唤醒睡眠线程。如果这样做,线程将抛出InterruptedException。

5)要调用sleep()方法,线程不需要对象锁定。

  1. wait()Object类的方法。
    sleep()Thread class。

  2. 的方法
  3. sleep允许线程进入it doesn’t release the lock状态x毫秒。
    当线程进入睡眠状态goes to suspended state时。

  4. notify()允许线程释放锁和notifAll()
    当<=>或<=>方法为时,此线程将处于活动状态 要求使用相同的对象。

One potential big difference between sleep/interrupt and wait/notify is that

Generating an exception when not needed is inefficient. If you have threads communicating with each other at a high rate, then it would be generating a lot of exceptions if you were calling interrupt all the time, which is a total waste of CPU.

You are correct - Sleep() causes that thread to "sleep" and the CPU will go off and process other threads (otherwise known as context switching) wheras I believe Wait keeps the CPU processing the current thread.

We have both because although it may seem sensible to let other people use the CPU while you're not using it, actualy there is an overhead to context switching - depending on how long the sleep is for, it can be more expensive in CPU cycles to switch threads than it is to simply have your thread doing nothing for a few ms.

Also note that sleep forces a context switch.

Also - in general it's not possible to control context switching - during the Wait the OS may (and will for longer waits) choose to process other threads.

The methods are used for different things.

Thread.sleep(5000);   // Wait until the time has passed.

Object.wait();        // Wait until some other thread tells me to wake up.

Thread.sleep(n) can be interrupted, but Object.wait() must be notified. It's possible to specify the maximum time to wait: Object.wait(5000) so it would be possible to use wait to, er, sleep but then you have to bother with locks.

Neither of the methods uses the cpu while sleeping/waiting.

The methods are implemented using native code, using similar constructs but not in the same way.

Look for yourself: Is the source code of native methods available? The file /src/share/vm/prims/jvm.cpp is the starting point...

Here wait() will be in the waiting state till it notify by another Thread but where as sleep() will be having some time..after that it will automatically transfer to the Ready state...

Wait() and sleep() Differences?

Thread.sleep() Once its work completed then only its release the lock to everyone. until its never release the lock to anyone.

  Sleep() take the key, its never release the key to anyone, when its work completed then only its release then only take the key waiting stage threads.

Object.wait() When its going to waiting stage, its will be release the key and its waiting for some of the seconds based on the parameter.

For Example:

you are take the coffee in yours right hand, you can take another anyone of the same hand, when will your put down then only take another object same type here. also. this is sleep() you sleep time you didn't any work, you are doing only sleeping.. same here also.

wait(). when you are put down and take another one mean while you are waiting , that's wait

you are play movie or anything in yours system same as player you can't play more than one at a time right, thats its here, when you close and choose another anyone movie or song mean while is called wait

wait releases the lock and sleep doesn't. A thread in waiting state is eligible for waking up as soon as notify or notifyAll is called. But in case of sleep the thread keeps the lock and it'll only be eligible once the sleep time is over.

sleep() method causes the current thread to move from running state to block state for a specified time. If the current thread has the lock of any object then it keeps holding it, which means that other threads cannot execute any synchronized method in that class object.

wait() method causes the current thread to go into block state either for a specified time or until notify, but in this case the thread releases the lock of the object (which means that other threads can execute any synchronized methods of the calling object.

In my opinion, the main difference between both mechanisms is that sleep/interrupt is the most basic way of handling threads, whereas wait/notify is an abstraction aimed to do thread inter-communication easier. This means that sleep/interrupt can do anything, but that this specific task is harder to do.

Why is wait/notify more suitable? Here are some personal considerations:

  1. It enforces centralization. It allows to coordinate the communication between a group of threads with a single shared object. This simplifies the work a lot.

  2. It enforces synchronization. Because it makes the programmer wrap the call to wait/notify in a synchronized block.

  3. It's independent of the thread origin and number. With this approach you can add more threads arbitrarily without editing the other threads or keeping a track of the existing ones. If you used sleep/interrupt, first you would need to keep the references to the sleeping threads, and then interrupt them one by one, by hand.

An example from the real life that is good to explain this is a classic restaurant and the method that the personnel use to communicate among them: The waiters leave the customer requests in a central place (a cork board, a table, etc.), ring a bell, and the workers from the kitchen come to take such requests. Once that there is any course ready, the kitchen personnel ring the bell again so that the waiters are aware and take them to the customers.

Example about sleep doesn’t release lock and wait does

Here there are two classes :

  1. Main : Contains main method and two threads.
  2. Singleton : This is singleton class with two static methods getInstance() and getInstance(boolean isWait).

    public class Main {
    
    private static Singleton singletonA = null;
    private static Singleton singletonB = null;
    
    public static void main(String[] args) throws InterruptedException {
    
    Thread threadA = new Thread() {
        @Override
        public void run() {
    
            singletonA = Singleton.getInstance(true);
    
        }
    };
    
    Thread threadB = new Thread() {
        @Override
        public void run() {
            singletonB = Singleton.getInstance();
    
            while (singletonA == null) {
                System.out.println("SingletonA still null");
            }
    
            if (singletonA == singletonB) {
                System.out.println("Both singleton are same");
            } else {
                System.out.println("Both singleton are not same");
            }
    
        }
    };
    
    threadA.start();
    threadB.start();
    
     }
    }
    

and

public class Singleton {

    private static Singleton _instance;

    public static Singleton getInstance() {

    if (_instance == null) {
        synchronized (Singleton.class) {
            if (_instance == null)
                _instance = new Singleton();
        }
    }
    return _instance;

}

public static Singleton getInstance(boolean isWait) {

    if (_instance == null) {
        synchronized (Singleton.class) {
            if (_instance == null) {
                if (isWait) {
                    try {
                        // Singleton.class.wait(500);//Using wait
                        Thread.sleep(500);// Using Sleep
                        System.out.println("_instance :"
                                + String.valueOf(_instance));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                _instance = new Singleton();
            }
        }
    }
    return _instance;

 }
}

Now run this example you will get below output :

_instance :null
Both singleton are same

Here Singleton instances created by threadA and threadB are same. It means threadB is waiting outside until threadA release it’s lock.

Now change the Singleton.java by commenting Thread.sleep(500); method and uncommenting Singleton.class.wait(500); . Here because of Singleton.class.wait(500); method threadA will release all acquire locks and moves into the “Non Runnable” state, threadB will get change to enter in synchronized block.

Now run again :

SingletonA still null
SingletonA still null
SingletonA still null
_instance :com.omt.sleepwait.Singleton@10c042ab
SingletonA still null
SingletonA still null
SingletonA still null
Both singleton are not same

Here Singleton instances created by threadA and threadB are NOT same because of threadB got change to enter in synchronised block and after 500 milliseconds threadA started from it’s last position and created one more Singleton object.

Should be called from synchronized block : wait() method is always called from synchronized block i.e. wait() method needs to lock object monitor before object on which it is called. But sleep() method can be called from outside synchronized block i.e. sleep() method doesn’t need any object monitor.

IllegalMonitorStateException : if wait() method is called without acquiring object lock than IllegalMonitorStateException is thrown at runtime, but sleep() method never throws such exception.

Belongs to which class : wait() method belongs to java.lang.Object class but sleep() method belongs to java.lang.Thread class.

Called on object or thread : wait() method is called on objects but sleep() method is called on Threads not objects.

Thread state : when wait() method is called on object, thread that holded object’s monitor goes from running to waiting state and can return to runnable state only when notify() or notifyAll() method is called on that object. And later thread scheduler schedules that thread to go from from runnable to running state. when sleep() is called on thread it goes from running to waiting state and can return to runnable state when sleep time is up.

When called from synchronized block : when wait() method is called thread leaves the object lock. But sleep() method when called from synchronized block or method thread doesn’t leaves object lock.

For More Reference

From oracle documentation page on wait() method of Object:

public final void wait()
  1. Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0).
  2. The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up
  3. interrupts and spurious wakeups are possible
  4. This method should only be called by a thread that is the owner of this object's monitor

This method throws

  1. IllegalMonitorStateException - if the current thread is not the owner of the object's monitor.

  2. InterruptedException - if any thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted status of the current thread is cleared when this exception is thrown.

From oracle documentation page on sleep() method of Thread class:

public static void sleep(long millis)
  1. Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers.
  2. The thread does not lose ownership of any monitors.

This method throws:

  1. IllegalArgumentException - if the value of millis is negative

  2. InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.

Other key difference:

wait() is a non-static method (instance method) unlike static method sleep() (class method).

wait() is given inside a synchronized method whereas sleep() is given inside a non-synchronized method because wait() method release the lock on the object but sleep() or yield() does release the lock().

wait with a timeout value can wakeup upon timeout value elapsed or notify whichever is earlier (or interrupt as well), whereas, a sleep wakes up on timeout value elapsed or interrupt whichever is earlier. wait() with no timeout value will wait for ever until notified or interrupted

  • The method wait(1000) causes the current thread to sleep up to one second.
    • A thread could sleep less than 1 second if it receives the notify() or notifyAll() method call.
  • The call to sleep(1000) causes the current thread to sleep for exactly 1 second.
    • Also sleeping thread doesn't hold lock any resource. But waiting thread does.
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top