package org.rui.thread.block;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; public class NotifyVsNotifyAll { public static void main(String[] args) throws InterruptedException {
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.println("\n notify() ");
Task.blocker.prod();//
prod = false;
} else {
System.out.println("\n notifyAll()");
Task.blocker.prodAll();
prod = true;
}
}
}, 400, 400);
TimeUnit.SECONDS.sleep(5);// run for a while...
timer.cancel();// 终止此计时器,
System.out.println("\n Timer canceled");
TimeUnit.MILLISECONDS.sleep(500);
System.out.println("Task.blocker.prodAll()");
Task2.blocker.prodAll();//唤醒task2 不包含不论什么在Task.blocker中的锁上等待的任务
TimeUnit.MILLISECONDS.sleep(500);
System.out.println("\nshutting down");
exec.shutdownNow(); }
} class Blocker {
synchronized void waitingCall() {
try {
while (!Thread.interrupted()) {
wait();
System.out.println(Thread.currentThread() + " ");
} } catch (InterruptedException e) {
// ok to exit this way
}
} synchronized void prod() {
notify();//在众多等候同一个锁的任务中。仅仅有一个会被唤醒,因此假设你希望使用nofify ,就必须保证被唤醒的是恰当的任务
} 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();
//System.out.println(Thread.currentThread()+" : Task 不会被换醒Task2");
} }
/**
* output:
*
notify()
Thread[pool-1-thread-2,5,main] notifyAll()
Thread[pool-1-thread-2,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-1,5,main] notify()
Thread[pool-1-thread-2,5,main] notifyAll()
Thread[pool-1-thread-2,5,main]
Thread[pool-1-thread-1,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-2,5,main] notifyAll()
Thread[pool-1-thread-2,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-1,5,main] notify()
Thread[pool-1-thread-2,5,main] notifyAll()
Thread[pool-1-thread-2,5,main]
Thread[pool-1-thread-1,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-2,5,main] notifyAll()
Thread[pool-1-thread-2,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-1,5,main] notify()
Thread[pool-1-thread-2,5,main] notifyAll()
Thread[pool-1-thread-2,5,main]
Thread[pool-1-thread-1,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
Task.blocker.prodAll()
Thread[pool-1-thread-6,5,main] shutting down
*/

java 线程 错失的信号、notify() 与notifyAll的使用的更多相关文章

  1. java 线程之间通信以及notify与notifyAll区别。

    jvm多个线程间的通信是通过 线程的锁.条件语句.以及wait().notify()/notifyAll组成. 下面来实现一个启用多个线程来循环的输出两个不同的语句. package com.app. ...

  2. 如何在 Java 中正确使用 wait, notify 和 notifyAll(转)

    wait, notify 和 notifyAll,这些在多线程中被经常用到的保留关键字,在实际开发的时候很多时候却并没有被大家重视.本文对这些关键字的使用进行了描述. 在 Java 中可以用 wait ...

  3. 如何在 Java 中正确使用 wait, notify 和 notifyAll – 以生产者消费者模型为例

    wait, notify 和 notifyAll,这些在多线程中被经常用到的保留关键字,在实际开发的时候很多时候却并没有被大家重视.本文对这些关键字的使用进行了描述. 在 Java 中可以用 wait ...

  4. 通过生产者消费者模式例子讲解Java基类方法wait、notify、notifyAll

    wait(),notify()和notifyAll()都是Java基类java.lang.Object的方法. 通俗解释wait():在当前线程等待其它线程唤醒.notify(): 唤醒一个线程正在等 ...

  5. 如何在 Java 中正确使用 wait, notify 和 notifyAll?

    简介     wait,notify,notifyAll,都是属于object对象提供的方法,但在实际工作中怎么使用这几个方法,确是很多程序员清楚,不够明白,在群里问,有人说,哪个线程想wait,就用 ...

  6. Java多线程编程——wait()和notify()、notifyAll()

    1.源码 wait() notify() notifyAll()都是Object类中方法.源码如下所示: public final native void notify(); public final ...

  7. 006 Java并发编程wait、notify、notifyAll和Condition

    原文https://www.cnblogs.com/dolphin0520/p/3920385.html#4182690 Java并发编程:线程间协作的两种方式:wait.notify.notifyA ...

  8. Java线程之wait()、notify()、notifyAll()

    翻译:https://www.journaldev.com/1037/java-thread-wait-notify-and-notifyall-example 简述 java中Objct对象包含三个 ...

  9. 重学JAVA基础(七):线程的wait、notify、notifyAll、sleep

    /** * 测试thread的wait notify notifyAll sleep Interrupted * @author tomsnail * @date 2015年4月20日 下午3:20: ...

随机推荐

  1. compare正序与逆序

    //list:在数据查询出来的Record集合 //juli:是需要比较的字段   //实现一个Comparator接口 //后面减去前面是正序   前面减去后面是倒叙 //我这里做的一个距离排序 R ...

  2. 微信js sdk上传多张图片

    微信js sdk上传多张图片,微信上传多张图片 该案例已tp3.2商城为例 直接上代码: php代码: public function ind(){ $appid="111111111111 ...

  3. im4java+GraphicsMagick

    package com.jeeplus.modules.isp.utils; import java.io.ByteArrayInputStream; import java.io.ByteArray ...

  4. 优先队列 + 并查集 + 字典树 + 欧拉回路 + 树状数组 + 线段树 + 线段树点更新 + KMP +AC自动机 + 扫描线

    这里给出基本思想和实现代码 . 优先队列 : 曾经做过的一道例题       坦克大战 struct node { int x,y,step; friend bool operator <(no ...

  5. Unity5.3.6升级到Unity5.4.4 NGUI出现Ignoring menu item NGUI because it is in no submenu!问题解决方案

    目录Assets/Plugins/NGUI/Scripts/Editor/NGUIMenu.cs文件中找到下图(左)所示,改成(右)图所示

  6. 题解报告:hdu 1850 Being a Good Boy in Spring Festival(尼姆博弈)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1850 Problem Description 一年在外 父母时刻牵挂春节回家 你能做几天好孩子吗寒假里 ...

  7. xhtml1-transitional.dtd

    <!-- Extensible HTML version 1.0 Transitional DTD This is the same as HTML 4 Transitional except ...

  8. α&β测试的定义及结束的标准

    α测试在系统开发接近完成时对应用系统的测试:测试后仍然会有少量的设计变更.这种测试一般由最终用户或其他人员完成,不能由程序或测试员完成. β测试当开发和测试根本完成时所做的用例,最终的错误和问题需要在 ...

  9. 开启远程MySQL

    安装完MySQL,由于安全原因默认是没有赋予用户远程权限的,所以第一步要首先赋予用户对应的权限 一  授权 mysql> mysql -u用户名 [-pIp地址] -p #登录 mysql> ...

  10. 前端面试基础-html篇之H5新特性

    h5的新特性(目前个人所了解)如下 语义化标签 表单新特性 视频(video)和音频(audio) canvas画布 svg绘图 地理定位 为鼠标提供的拖放API webworker (重点)Stor ...