博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
线程 wait 等待与notify 唤醒 使用 java 代码
阅读量:5057 次
发布时间:2019-06-12

本文共 7553 字,大约阅读时间需要 25 分钟。

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
threadWaitNotify = new ConcurrentHashMap
(0); public static Map
resultMaps = new HashMap
(); 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"); } } }}

 

转载于:https://www.cnblogs.com/xiaolei2017/p/9198364.html

你可能感兴趣的文章
Range和xrange的区别
查看>>
BZOJ 1010 [HNOI2008]玩具装箱 (斜率优化DP)
查看>>
java-动态规划算法学习笔记
查看>>
STL容器之vector
查看>>
Linux 内核中断内幕
查看>>
DNS负载均衡
查看>>
无法向会话状态服务器发出会话状态请求
查看>>
数据中心虚拟化技术
查看>>
Oracle OEM 配置报错: No value was set for the parameter DBCONTROL_HTTP_PORT 解决方法
查看>>
01入门
查看>>
python正则表达式
查看>>
嵌套循环连接(nested loops join)原理
查看>>
shell统计特征数量
查看>>
复习文件操作
查看>>
C#Hashtable与Dictionary性能
查看>>
10个让你忘记 Flash 的 HTML5 应用演示
查看>>
8个Python面试必考的题目,小编也被坑过 ToT
查看>>
SQL Server 使用作业设置定时任务之一(转载)
查看>>
centos 图形界面和命令行界面切换(转载)
查看>>
Maven启用代理访问
查看>>