zoukankan      html  css  js  c++  java
  • Java并发编程系列之三十二:丢失的信号

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

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

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

    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:一是全部等待线程的类型都同样,这就是说,等待队列仅仅与一个条件变量相关,而且全部的线程在唤醒后运行的都是同样的操作;二是单进单出,也就是说在条件变量的每一个通知,要求仅仅能最多唤醒一个线程
  • 相关阅读:
    Android应用安全之外部动态加载DEX文件风险
    Android应用安全之Android APP通用型拒绝服务漏洞
    利用Cydia Substrate进行Android HOOK(二)
    Apk去签名校验详解
    nginx部署前端项目的一些配置【刚入门】
    java 日期格式化 将String日期重新格式化成String型【转】
    idea导入maven项目时需要注意
    element 'dependency' cannot have character [children], because the type's content type is element-only.
    腾讯云centos7执行 systemctl status postfix.service 启动postfix服务时出现 /usr/sbin/postconf: fatal: parameter inet_interfaces: no local interface found for ::1
    redis.clients.jedis.exceptions.JedisExhaustedPoolException: Could not get a resource since the pool is exhausted
  • 原文地址:https://www.cnblogs.com/yxysuanfa/p/7399428.html
Copyright © 2011-2022 走看看