基于Java多线程notify与notifyall的区别分析


-
本篇文章对Java中多线程notify与notifyall的区别进行了详细的分析介绍。需要的朋友参考下
-
2020-09-05
微信JsAPI(java)支付,支付完成后notify_url无法调用_course
2015-06-02最近做微信支付,以前也没做过微信方面的东西,所以都不懂。。 只能求助各位大神了。。 微信文档中说明,在支付成功后会根据notify_url中设置的地址通知商户支付成功的信息, 但是现在不管是将地址设置为xxx.do还是设置为授权目录下的xxx.jsp都不行。 麻烦各位做过的帮忙解答下吧。 谢谢。
微信支付notify_url回调_course
2015-04-29package com.tra.action; import java.io.BufferedOutputStream; import java.io.IOException; import java
java 并发基础 notify()与notifyAll() 的理解_course
2019-09-24代码来自java 编程思想第4版 21章 * 代码1 ```java //: concurrency/NotifyVsNotifyAll.java package concurrency; /* Added by Eclipse.py */ import java.util.concurrent.*; import java.util.*; class Blocker { synchronized void waitingCall() { try { while (!Thread.interrupted()) { wait(); System.out.print(Thread.currentThread() + " "); } } catch (InterruptedException e) { // OK to exit this way } } synchronized void prod() { notify(); } synchronized void prodAll() { notifyAll(); } } class Task implements Runnable { static Blocker blocker = new Blocker(); public void run() { blocker.waitingCall(); } } class Task2 implements Runnable { // A separate Blocker object: static Blocker blocker = new Blocker(); public void run() { blocker.waitingCall(); } } public class NotifyVsNotifyAll { public static void main(String[] args) throws Exception { ExecutorService exec = Executors.newCachedThreadPool(); for (int i = 0; i < 5; i++) exec.execute(new Task()); exec.execute(new Task2()); Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { boolean prod = true; public void run() { if (prod) { System.out.print("\nnotify() "); Task.blocker.prod(); prod = false; } else { System.out.print("\nnotifyAll() "); Task.blocker.prodAll(); prod = true; } } }, 400, 400); // Run every .4 second TimeUnit.SECONDS.sleep(5); // Run for a while... timer.cancel(); System.out.println("\nTimer canceled"); TimeUnit.MILLISECONDS.sleep(500); System.out.print("Task2.blocker.prodAll() "); Task2.blocker.prodAll(); TimeUnit.MILLISECONDS.sleep(500); System.out.println("\nShutting down"); exec.shutdownNow(); // Interrupt all tasks } } /* * Output: (Sample) notify() Thread[pool-1-thread-1,5,main] notifyAll() * Thread[pool-1-thread-1,5,main] Thread[pool-1-thread-5,5,main] * Thread[pool-1-thread-4,5,main] Thread[pool-1-thread-3,5,main] * Thread[pool-1-thread-2,5,main] notify() Thread[pool-1-thread-1,5,main] * notifyAll() Thread[pool-1-thread-1,5,main] Thread[pool-1-thread-2,5,main] * Thread[pool-1-thread-3,5,main] Thread[pool-1-thread-4,5,main] * Thread[pool-1-thread-5,5,main] notify() Thread[pool-1-thread-1,5,main] * notifyAll() Thread[pool-1-thread-1,5,main] Thread[pool-1-thread-5,5,main] * Thread[pool-1-thread-4,5,main] Thread[pool-1-thread-3,5,main] * Thread[pool-1-thread-2,5,main] notify() Thread[pool-1-thread-1,5,main] * notifyAll() Thread[pool-1-thread-1,5,main] Thread[pool-1-thread-2,5,main] * Thread[pool-1-thread-3,5,main] Thread[pool-1-thread-4,5,main] * Thread[pool-1-thread-5,5,main] notify() Thread[pool-1-thread-1,5,main] * notifyAll() Thread[pool-1-thread-1,5,main] Thread[pool-1-thread-5,5,main] * Thread[pool-1-thread-4,5,main] Thread[pool-1-thread-3,5,main] * Thread[pool-1-thread-2,5,main] notify() Thread[pool-1-thread-1,5,main] * notifyAll() Thread[pool-1-thread-1,5,main] Thread[pool-1-thread-2,5,main] * Thread[pool-1-thread-3,5,main] Thread[pool-1-thread-4,5,main] * Thread[pool-1-thread-5,5,main] Timer canceled Task2.blocker.prodAll() * Thread[pool-1-thread-6,5,main] Shutting down */// :~ ``` * 代码2 ```java //: concurrency/Restaurant.java package concurrency; /* Added by Eclipse.py */ // The producer-consumer approach to task cooperation. import java.util.concurrent.*; import static net.mindview.util.Print.*; class Meal { private final int orderNum; public Meal(int orderNum) { this.orderNum = orderNum; } public String toString() { return "Meal " + orderNum; } } class WaitPerson implements Runnable { private Restaurant restaurant; public WaitPerson(Restaurant r) { restaurant = r; } public void run() { try { while (!Thread.interrupted()) { synchronized (this) { while (restaurant.meal == null) wait(); // ... for the chef to produce a meal } print("Waitperson got " + restaurant.meal); synchronized (restaurant.chef) { restaurant.meal = null; restaurant.chef.notifyAll(); // Ready for another //这里的notifyAll();/////////////////////// } } } catch (InterruptedException e) { print("WaitPerson interrupted"); } } } class Chef implements Runnable { private Restaurant restaurant; private int count = 0; public Chef(Restaurant r) { restaurant = r; } public void run() { try { while (!Thread.interrupted()) { synchronized (this) { while (restaurant.meal != null) wait(); // ... for the meal to be taken } if (++count == 10) { print("Out of food, closing"); restaurant.exec.shutdownNow(); } printnb("Order up! "); synchronized (restaurant.waitPerson) { restaurant.meal = new Meal(count); restaurant.waitPerson.notifyAll();-////////////// } TimeUnit.MILLISECONDS.sleep(100); } } catch (InterruptedException e) { print("Chef interrupted"); } } } public class Restaurant { Meal meal; ExecutorService exec = Executors.newCachedThreadPool(); WaitPerson waitPerson = new WaitPerson(this); Chef chef = new Chef(this); public Restaurant() { exec.execute(chef); exec.execute(waitPerson); } public static void main(String[] args) { new Restaurant(); } } /* * Output: Order up! Waitperson got Meal 1 Order up! Waitperson got Meal 2 Order * up! Waitperson got Meal 3 Order up! Waitperson got Meal 4 Order up! * Waitperson got Meal 5 Order up! Waitperson got Meal 6 Order up! Waitperson * got Meal 7 Order up! Waitperson got Meal 8 Order up! Waitperson * got Meal 9 * Out of food, closing WaitPerson interrupted Order up! Chef interrupted */// :~ ``` * 问题 1. notifyAll是Object的方法 ---public final native void notifyAll();但不是静态方法 这个在代码1中,为什么没有实例对象就可以直接调用该方法 而在代码2中为什么需要 restaurant.waitPerson.notifyAll() ;如果直接改成notifyAll(); 会报错 ``` Order up! Exception in thread "pool-1-thread-1" java.lang.IllegalMonitorStateException at java.lang.Object.notifyAll(Native Method) at concurrency.Chef.run(Restaurant.java:69) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) ``` 2. 这2种调用的方法有什么区别
-
下载
4.3.listwidget 添加城市
4.3.listwidget 添加城市
-
博客
习题7-4(uva-818)
习题7-4(uva-818)
-
博客
2021-02-28
2021-02-28
-
学院
PPT大神之路高清教程
PPT大神之路高清教程
-
下载
6.1.共享内存界面设计
6.1.共享内存界面设计
-
博客
SDMtool工具使用教程-基于最大熵Maxent-ArcGis地理分布预测高级分析内容 更新于2021.2.28
SDMtool工具使用教程-基于最大熵Maxent-ArcGis地理分布预测高级分析内容 更新于2021.2.28
-
学院
Amoeba 实现 MySQL 高可用、负载均衡和读写分离
Amoeba 实现 MySQL 高可用、负载均衡和读写分离
-
博客
04_STM32CubeMX学习-定时器
04_STM32CubeMX学习-定时器
-
学院
基于Flink+Hudi构建企业亿级云上实时数据湖教程(PC、移动、小
基于Flink+Hudi构建企业亿级云上实时数据湖教程(PC、移动、小
-
下载
HGM3000消防应急广播设备使用说明书.pdf
HGM3000消防应急广播设备使用说明书.pdf
-
学院
鸿蒙系统Harmonyos源码架构分析-第1期第2课
鸿蒙系统Harmonyos源码架构分析-第1期第2课
-
博客
Ntsys软件教程-PAUP软件教程之基于01矩阵数据建树分析
Ntsys软件教程-PAUP软件教程之基于01矩阵数据建树分析
-
下载
快手无人直播工具包
快手无人直播工具包
-
学院
MySQL 管理利器 mysql-utilities
MySQL 管理利器 mysql-utilities
-
下载
2020-L1V3.pdf
2020-L1V3.pdf
-
下载
6.2.创建共享内存写入数据到缓冲区
6.2.创建共享内存写入数据到缓冲区
-
学院
JMETER 性能测试基础课程
JMETER 性能测试基础课程
-
博客
HTML5音频播放,指定时间段播放
HTML5音频播放,指定时间段播放
-
下载
狂神的SpringSecurity素材.rar
狂神的SpringSecurity素材.rar
-
下载
HGM2001消防应急广播设备使用说明书.pdf
HGM2001消防应急广播设备使用说明书.pdf
-
博客
FC
FC
-
博客
linux查询程序PID和批量程序PID
linux查询程序PID和批量程序PID
-
学院
龙芯生态应用开发基础:C语言精要
龙芯生态应用开发基础:C语言精要
-
博客
通过pytorch建立神经网络模型 分析遗传基因数据
通过pytorch建立神经网络模型 分析遗传基因数据
-
学院
华为1+X——网络系统建设与运维(高级)
华为1+X——网络系统建设与运维(高级)
-
博客
数据库SQL语句基础
数据库SQL语句基础
-
博客
2021-02-28
2021-02-28
-
学院
实现 MySQL 读写分离的利器 mysql-proxy
实现 MySQL 读写分离的利器 mysql-proxy
-
学院
基于电商业务的全链路数据中台落地方案(全渠道、全环节、全流程)
基于电商业务的全链路数据中台落地方案(全渠道、全环节、全流程)
-
下载
北京邮电大学算法设计与分析课件.zip
北京邮电大学算法设计与分析课件.zip