首页 技术 正文
技术 2022年11月9日
0 收藏 874 点赞 3,743 浏览 2795 个字

这里的丢失的信号是指线程必须等待一个已经为真的条件,在開始等待之前没有检查等待条件。这样的场景事实上挺好理解,假设一边烧水,一边看电视,那么在水烧开的时候。由于太投入而没有注意到水被烧开。

丢失的信号指的就是这样的情况。

创建两个线程分别运行通知和等待方法,而且将运行通知的线程先与运行等待的线程,以下的代码演示了这点:

package com.rhwayfun.patchwork.concurrency.r0414;import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;/**
* Created by rhwayfun on 16-4-14.
*/
public class MissedNotifyDemo { //持有的锁
private static Object lock = new Object();
//日期格式器
private static final DateFormat format = new SimpleDateFormat("HH:mm:ss"); //等待线程运行的方法
public void waitMethod() throws InterruptedException {
System.out.println(Thread.currentThread().getName() + ": enter waitMethod at "
+ format.format(new Date()));
synchronized (lock){
//调用wait方法运行等待
System.out.println(Thread.currentThread().getName() + ": start invoke wait method at "
+ format.format(new Date()));
lock.wait();
System.out.println(Thread.currentThread().getName() + ": end invoke wait method at "
+ format.format(new Date()));
}
System.out.println(Thread.currentThread().getName() + ": exit waitMethod at "
+ format.format(new Date()));
} //通知线程运行的方法
public void notifyMethod(){
System.out.println(Thread.currentThread().getName() + ": exit notifyMethod at "
+ format.format(new Date()));
synchronized (lock){
//调用通知的方法
System.out.println(Thread.currentThread().getName() + ": start invoke notify method at "
+ format.format(new Date()));
lock.notifyAll();
System.out.println(Thread.currentThread().getName() + ": end invoke notify method at "
+ format.format(new Date()));
}
System.out.println(Thread.currentThread().getName() + ": exit notifyMethod at "
+ format.format(new Date()));
} static class WaitThread implements Runnable{
private MissedNotifyDemo missedNotifyDemo; public WaitThread(MissedNotifyDemo missedNotifyDemo) {
this.missedNotifyDemo = missedNotifyDemo;
} @Override
public void run() {
try {
TimeUnit.MILLISECONDS.sleep(1000);
missedNotifyDemo.waitMethod();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} static class NotifyThread implements Runnable{ private MissedNotifyDemo missedNotifyDemo; public NotifyThread(MissedNotifyDemo missedNotifyDemo) {
this.missedNotifyDemo = missedNotifyDemo;
} @Override
public void run() {
try {
//休眠的时间必须要小于等待线程的休眠时间
TimeUnit.MILLISECONDS.sleep(500);
missedNotifyDemo.notifyMethod();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} public static void main(String[] args){
MissedNotifyDemo missedNotifyDemo = new MissedNotifyDemo();
new Thread(new WaitThread(missedNotifyDemo),"WaitThread").start();
new Thread(new NotifyThread(missedNotifyDemo),"NotifyThread").start();
}
}

运行结果例如以下:

WaitThread由于丢失了来自NotifyThread的通知而一直陷入等待中。当然,这里仅仅是演示了这样的情况。在实际的样例中,运行等待的线程都须要一个等待条件。为了避免出现丢失的信号。仍然须要对条件变量进行while循环的推断。

关于等待通知机制的补充

  1. 每当在等待一个条件时。一定要确保在条件变量变为真的时候才发出唤醒的通知
  2. 在调用wait/notify/notifyAll方法时。必须首先获得锁
  3. 每次调用完wait方法,获得锁就会自己主动释放
  4. 调用notify时,JVM从等待队列中的一个线程进行唤醒,调用notifyAll时,将等待队列中全部线程都唤醒
  5. 仅仅有同一时候满足两个条件时才干使用notify:一是全部等待线程的类型都同样,这就是说,等待队列仅仅与一个条件变量相关,而且全部的线程在唤醒后运行的都是同样的操作;二是单进单出,也就是说在条件变量的每一个通知,要求仅仅能最多唤醒一个线程
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295