zoukankan      html  css  js  c++  java
  • 线程 wait 等待与notify 唤醒 使用 java 代码

    package com.cfets.cfib.thread.wait;
    
    import java.util.HashMap;
    import java.util.Map;
    import java.util.concurrent.ConcurrentHashMap;
    
    /**
     * 线程等待通知机制  wait  notify
     * 线程等待通知机制 前提必须使用synchronized锁
     * Created by cfets on  2018/6/19.11:22
     */
    public class WaitTest2 {
    
        public static ConcurrentHashMap<String, Object> threadWaitNotify = new ConcurrentHashMap<String, Object>(0);
    
        public static Map<String, Object> resultMaps = new HashMap<String, Object>();
    
        public static WaitTest2 waitTest = new WaitTest2();
    
        /**
         * 模拟三个线程一直等待结果处理完后取出相应的结果
         *
         * @param args
         * @throws Exception
         */
        public static void main(String[] args) throws Exception {
    
            new Thread(() -> {
                String uuid1 = "OQ-NF-DHI-01";
                Object o = new Object();
                // 等待2秒
                ThreadWaitNotify threadWaitNotify = waitTest.new ThreadWaitNotify(2, uuid1);
                threadWaitNotify.start();
                synchronized (o) {
                    WaitTest2.threadWaitNotify.put(uuid1, o);
                    try {
                        System.out.println(" 线程 " + uuid1 + " wait 等待开始");
                        o.wait();
                        System.out.println(" 线程 " + uuid1 + " wait 等待结束");
                        System.out.println(" " + uuid1 + " 结果:" + waitTest.resultMaps.get(uuid1));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
    
            new Thread(() -> {
                String uuid2 = "OQ-NF-DHI-02";
                Object o2 = new Object();
                // 等待4秒
                ThreadWaitNotify threadWaitNotify = waitTest.new ThreadWaitNotify(4, uuid2);
                threadWaitNotify.start();
                synchronized (o2) {
                    WaitTest2.threadWaitNotify.put(uuid2, o2);
                    try {
                        System.out.println(" 线程 " + uuid2 + " wait 等待开始");
                        o2.wait();
                        System.out.println(" 线程 " + uuid2 + " wait 等待结束");
                        System.out.println(" " + uuid2 + " 结果:" + waitTest.resultMaps.get(uuid2));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
    
            new Thread(() -> {
                String uuid3 = "OQ-NF-DHI-03";
                Object o3 = new Object();
                // 等待6秒
                ThreadWaitNotify threadWaitNotify = waitTest.new ThreadWaitNotify(6, uuid3);
                threadWaitNotify.start();
                synchronized (o3) {
                    waitTest.threadWaitNotify.put(uuid3, o3);
                    try {
                        System.out.println(" 线程 " + uuid3 + " wait 等待开始");
                        o3.wait();
                        System.out.println(" 线程 " + uuid3 + " wait 等待结束");
                        System.out.println(" " + uuid3 + " 结果:" + waitTest.resultMaps.get(uuid3));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    
        public class ThreadWaitNotify extends Thread {
    
            public int waitime = 0;
            public String name = null;
    
            public ThreadWaitNotify(int waitime, String name) {
                this.waitime = waitime;
                this.name = name;
            }
    
            @Override
            public void run() {
                try {
                    Thread.sleep(waitime * 1000);
                    Object value = threadWaitNotify.get(name);
                    waitTest.resultMaps.put(name, new Object().toString());
                    if (value != null) {
                        synchronized (value) {
                            value.notify();
                            System.out.println();
                            System.out.println(" 线程 " + name + " notify 唤醒");
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    
    ----------------------------------------------------------------------------
    package com.cfets.cfib.thread.wait;
    
    /**
     * Created by cfets on  2018/6/19.12:20
     */
    public class WaitNotifyTest {
    
        //在多线程间共享的对象上使用wait
        private String[] shareObj = {"true"};
    
        public static void main(String[] args) {
            WaitNotifyTest test = new WaitNotifyTest();
            ThreadWait threadWait1 = test.new ThreadWait("wait thread1");
            threadWait1.setPriority(2);
            ThreadWait threadWait2 = test.new ThreadWait("wait thread2");
            threadWait2.setPriority(3);
            ThreadWait threadWait3 = test.new ThreadWait("wait thread3");
            threadWait3.setPriority(4);
    
            threadWait1.start();
            threadWait2.start();
            threadWait3.start();
    
            ThreadNotify threadNotify = test.new ThreadNotify("notify thread");
            threadNotify.start();
        }
    
        class ThreadWait extends Thread {
    
            public ThreadWait(String name) {
                super(name);
            }
    
            public void run() {
                synchronized (shareObj) {
                    if ("true".equals(shareObj[0])) {
                        System.out.println("线程 " + this.getName() + " 开始等待");
                        long startTime = System.currentTimeMillis();
                        try {
                            // 线程等待 知道notify唤醒
                            shareObj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        long endTime = System.currentTimeMillis();
                        System.out.println("线程 " + this.getName() + " 等待时间为:" + (endTime - startTime) / 1000 + " 秒");
                    }
                }
                System.out.println("线程 " + getName() + " 等待结束");
            }
        }
    
        class ThreadNotify extends Thread {
    
            public ThreadNotify(String name) {
                super(name);
            }
    
            public void run() {
                try {
                    // 给等待线程等待时间
                    sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (shareObj) {
                    System.out.println("------------------------------");
                    System.out.println("线程 " + this.getName() + "开始准备通知");
                    shareObj[0] = "false";
                    shareObj.notifyAll();
                    System.out.println("线程 " + this.getName() + "通知结束");
                }
                System.out.println("线程 " + this.getName() + "运行结束");
                System.out.println("------------------------------");
            }
        }
    }
    
    ----------------------------------------------------------------------------
    package com.cfets.cfib.thread.wait;
    
    /**
     * Created by cfets on  2018/6/19.9:06
     */
    public class WaitTest {
    
        public static void main(String[] args) {
            ThreadA t1 = new ThreadA("t1");
    
            synchronized (t1) {
                try {
                    System.out.println(Thread.currentThread().getName() + " start t1");
                    t1.start();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    
        }
    
        public static class ThreadA extends Thread {
    
            public ThreadA(String name) {
                super(name);
            }
    
            public void run() {
                synchronized (this) {
                    System.out.println(Thread.currentThread().getName() + " call notify()");
                    try {
                        System.out.println(Thread.currentThread().getName() + " wait()");
    //                    this.wait();
                        this.wait(2000); // 2秒后自动唤醒
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + " continue");
                }
            }
        }
    }
  • 相关阅读:
    Analog power pin UPF defination
    动态功耗计算
    静态功耗 计算
    Innovus 对multibit 的支持
    P &R 12
    P & R 11
    power-plan如何定
    P & R 10
    P & R 9
    线程基础
  • 原文地址:https://www.cnblogs.com/xiaolei2017/p/9198364.html
Copyright © 2011-2022 走看看