面试常考点:虚假唤醒 Spurious wakeup

https://en.wikipedia.org/wiki/Spurious_wakeup

Spurious wakeup describes a complication in the use of condition variables as provided by certain multithreading APIs such as POSIX Threads and the Windows API.

Even after a condition variable appears to have been signaled from a waiting thread's point of view, the condition that was awaited may still be false. One of the reasons for this is a spurious wakeup; that is, a thread might be awoken from its waiting state even though no thread signaled the condition variable. For correctness it is necessary, then, to verify that the condition is indeed true after the thread has finished waiting. Because spurious wakeup can happen repeatedly, this is achieved by waiting inside a loop that terminates when the condition is true, for example:

/* In any waiting thread: */
while(!buf->full)
	wait(&buf->cond, &buf->lock);

/* In any other thread: */
if(buf->n >= buf->size){
	buf->full = 1;
	signal(&buf->cond);
}

In this example it is known that another thread will set buf->full (the actual condition awaited) before signaling buf->cond (the means of synchronizing the two threads). The waiting thread will always verify the truth of the actual condition upon returning from wait, ensuring correct behaviour if spurious wakeup occurs.

According to David R. Butenhof's Programming with POSIX Threads ISBN 0-201-63392-2:

"This means that when you wait on a condition variable, the wait may (occasionally) return when no thread specifically broadcast or signaled that condition variable. Spurious wakeups may sound strange, but on some multiprocessor systems, making condition wakeup completely predictable might substantially slow all condition variable operations. The race conditions that cause spurious wakeups should be considered rare."


http://siwind.iteye.com/blog/1469216

虚假唤醒(spurious wakeup)在采用条件等待时,我们使用的是 

Java代码   收藏代码
  1. while(条件不满足){  
  2.    condition_wait(cond, mutex);  
  3. }  
  4. 而不是:  
  5. If( 条件不满足 ){  
  6.    Condition_wait(cond,mutex);  
  7. }   


这是因为可能会存在虚假唤醒”spurious wakeup”的情况。
 
也就是说,即使没有线程调用condition_signal, 原先调用condition_wait的函数也可能会返回。此时线程被唤醒了,但是条件并不满足,这个时候如果不对条件进行检查而往下执行,就可能会导致后续的处理出现错误。 
虚假唤醒在linux的多处理器系统中/在程序接收到信号时可能回发生。在Windows系统和JAVA虚拟机上也存在。在系统设计时应该可以避免虚假唤醒,但是这会影响条件变量的执行效率,而既然通过while循环就能避免虚假唤醒造成的错误,因此程序的逻辑就变成了while循环的情况。 
注意:即使是虚假唤醒的情况,线程也是在成功锁住mutex后才能从condition_wait()中返回。即使存在多个线程被虚假唤醒,但是也只能是一个线程一个线程的顺序执行,也即:lock(mutex)   检查/处理  condition_wai()或者unlock(mutex)来解锁.  

### 回答1: 在Java中,虚假唤醒spurious wakeup)是指一个等待线程在没有收到明确的通知或信号的情况下从等待状态中返回。这可能是由于系统噪声、编译器优化或其他原因导致的。虽然这种情况很少发生,但是在多线程编程中需要注意处理虚假唤醒的情况,以确保程序的正确性和可靠性。为了避免虚假唤醒,可以使用循环等待模式来检查等待条件是否满足,而不是仅仅依赖于wait()方法的返回。例如,在使用wait()方法等待某个条件时,可以使用while循环来检查等待条件是否满足,而不是使用if语句。 ### 回答2: Java中的虚假唤醒是指在多线程环境下,一个或多个线程在等待条件达成时被错误地唤醒。通情况下,当一个线程调用对象的wait()方法后,它会进入等待状态,直到其他线程调用对象的notify()或notifyAll()方法来唤醒它。 然而,在某些情况下,虽然没有线程调用notify()或notifyAll()方法,等待线程仍然被唤醒。这种被错误地唤醒的情况就被称为虚假唤醒虚假唤醒可能会导致程序逻辑出现问题,因为等待线程在条件未满足的情况下提前被唤醒,从而导致程序执行不正或结果不准确。 虚假唤醒可能发生在以下情况下: 1. 当多个线程等待同一个条件时,某个线程被唤醒,但条件仍未满足。此时其他线程可能被错误地唤醒。 2. 当等待线程在等待过程中被中断,然后重新竞争到锁。虽然它们之前已经收到了中断信号,但由于虚假唤醒的存在,它们会被错误地唤醒。 为了防止虚假唤醒的发生,通在Java中使用循环来检查条件,而不仅仅依赖于等待和唤醒操作。例如,可以使用while循环来检查条件是否满足,如果条件不满足,则继续等待。这样可以避免虚假唤醒带来的问题,并保证程序的正确性和可靠性。 ### 回答3: 在Java中,虚假唤醒是指当一个线程在等待其他线程发送通知时,被唤醒但却没有相应的通知的情况。虚假唤醒可能会发生在使用Object类的wait()、notify()和notifyAll()方法时。 虚假唤醒是由于多线程环境下的竞态条件引起的。假设有多个线程等待一个共享资源,可能会出现这样的情况:一个线程被唤醒,但是在它检查共享资源之前,另一个线程已经修改了该资源的状态,导致出现错误的结果。 为了防止虚假唤醒产生的问题,应该在一个while循环中使用wait()方法来等待通知,而不是使用if语句。在等待之前,线程应该检查等待条件,并在等待条件发生变化之前循环等待,以防止在虚假唤醒的情况下继续执行。 虚假唤醒是多线程编程中需要注意的一个陷阱。为了避免虚假唤醒产生的问题,可以使用锁对象的条件变量来处理线程的等待和唤醒操作,如使用ReentrantLock类中的Condition对象。这样可以更精确地控制线程的等待和唤醒,避免虚假唤醒的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值