Java 线程第三版 第四章 Thread Notification 读书笔记
一、等待与通知
public final void wait() throws InterruptedException
等待条件的发生。假设通知没有在timeout指定的时间内发生,它还是会返回。
假设通知没有在timeout指定的毫秒与纳秒内发生。它还是会返回。
通知正在等待的Thread此条件已经发生。
public final void notifyAll()
public class AnimatedCharacterDisplayCanvas extends CharacterDisplayCanvas implements CharacterListener, Runnable {
private boolean done = true;
private int curX = 0;
private Thread timer = null;
public AnimatedCharacterDisplayCanvas() {
}
public AnimatedCharacterDisplayCanvas(CharacterSource cs) {
super(cs);
}
public synchronized void newCharacter(CharacterEvent ce) {
curX = 0;
tmpChar[0] = (char) ce.character;
repaint();
}
protected synchronized void paintComponent(Graphics gc) {
Dimension d = getSize();
gc.clearRect(0, 0, d.width, d.height);
if (tmpChar[0] == 0)
return;
int charWidth = fm.charWidth(tmpChar[0]);
gc.drawChars(tmpChar, 0, 1,
curX++, fontHeight);
}
public synchronized void run() {
while (true) {
try {
if (done) {
wait();
} else {
repaint();
wait(100);
}
} catch (InterruptedException ie) {
return;
}
}
}
public synchronized void setDone(boolean b) {
done = b;
if (timer == null) {
timer = new Thread(this);
timer.start();
}
if (!done)
notify();
}
}
可是Object class 提供另外一个方法notifyAll
全部等待中Thread都会被唤醒,但它们都还没要又一次取得对象的lock。所以这些Thread并非并行地运行:它们都必须等待对象lock被释放掉。
因此,一次仅仅有一个Thread可以运行,且仅仅有在调用notifyAll方法的这个Thread释放掉它的lock之后。
public class AnimatedCharacterDisplayCanvas extends CharacterDisplayCanvas implements CharacterListener, Runnable {
private boolean done = true;
private int curX = 0;
private Thread timer = null;
private Object doneLock = new Object();
public AnimatedCharacterDisplayCanvas() {
}
public AnimatedCharacterDisplayCanvas(CharacterSource cs) {
super(cs);
}
public synchronized void newCharacter(CharacterEvent ce) {
curX = 0;
tmpChar[0] = (char) ce.character;
repaint();
}
protected synchronized void paintComponent(Graphics gc) {
Dimension d = getSize();
gc.clearRect(0, 0, d.width, d.height);
if (tmpChar[0] == 0)
return;
int charWidth = fm.charWidth(tmpChar[0]);
gc.drawChars(tmpChar, 0, 1,
curX++, fontHeight);
}
public void run() {
synchronized(doneLock) {
while (true) {
try {
if (done) {
doneLock.wait();
} else {
repaint();
doneLock.wait(100);
}
} catch (InterruptedException ie) {
return;
}
}
}
}
public void setDone(boolean b) {
synchronized(doneLock) {
done = b;
if (timer == null) {
timer = new Thread(this);
timer.start();
}
if (!done)
doneLock.notify();
}
}
}
二、条件变量
POSIX条件变量的四个基本函数:wait(), thimeed_wait(), signal, broadcast。直接相应Java提供的wait(), wait(long),notify(),notifyAll
此类食欲Lock interface并用。由于这个新的interface是与调用以及lock对象分开的,它在用法上的灵活性就如筒其它Threading系统中的条件变量一样。
public class RandomCharacterGenerator extends Thread implements CharacterSource {
private static char[] chars;
private static String charArray = "abcdefghijklmnopqrstuvwxyz0123456789";
static {
chars = charArray.toCharArray();
}
private Random random;
private CharacterEventHandler handler;
private boolean done = true;
private Lock lock = new ReentrantLock();
private Condition cv = lock.newCondition();
public RandomCharacterGenerator() {
random = new Random();
handler = new CharacterEventHandler();
}
public int getPauseTime() {
return (int) (Math.max(1000, 5000 * random.nextDouble()));
}
public void addCharacterListener(CharacterListener cl) {
handler.addCharacterListener(cl);
}
public void removeCharacterListener(CharacterListener cl) {
handler.removeCharacterListener(cl);
}
public void nextCharacter() {
handler.fireNewCharacter(this,
(int) chars[random.nextInt(chars.length)]);
}
public void run() {
try {
lock.lock();
while (true) {
try {
if (done) {
cv.await();
} else {
nextCharacter();
cv.await(getPauseTime(), TimeUnit.MILLISECONDS);
}
} catch (InterruptedException ie) {
return;
}
}
} finally {
lock.unlock();
}
}
public void setDone(boolean b) {
try {
lock.lock();
done = b;
if (!done) cv.signal();
} finally {
lock.unlock();
}
}
}
对每个Lock对象都能够创建一个以上的Condition对象。这表示我们能够针对个别的Thread或ThreadGroup进行独立的设定。
与await()不同,它的调用不可能被中断。
Java 线程第三版 第四章 Thread Notification 读书笔记的更多相关文章
- Java 线程第三版 第五章 极简同步技巧 读书笔记
一.能避免同步吗? 取得锁会由于下面原因导致成本非常高: 取得由竞争的锁须要在虚拟机的层面上执行很多其它的程序代码. 要取得有竞争锁的线程总是必须等到锁被释放后. 1. 寄存器的效应 ...
- 《Java并发编程实战》第六章 任务运行 读书笔记
一. 在线程中运行任务 无限制创建线程的不足 .线程生命周期的开销很高 .资源消耗 .稳定性 二.Executor框架 Executor基于生产者-消费者模式.提交任务的操作相当于生产者.运行任务的线 ...
- 《Java并发编程实战》第五章 同步容器类 读书笔记
一.同步容器类 1. 同步容器类的问题 线程容器类都是线程安全的.可是当在其上进行符合操作则须要而外加锁保护其安全性. 常见符合操作包括: . 迭代 . 跳转(依据指定顺序找到当前元素的下一个元素) ...
- Java 线程第三版 第一章Thread导论、 第二章Thread的创建与管理读书笔记
第一章 Thread导论 为何要用Thread ? 非堵塞I/O I/O多路技术 轮询(polling) 信号 警告(Alarm)和定时器(Timer) 独立的任务(Ta ...
- Java 线程第三版 第九章 Thread调度 读书笔记
一.Thread调度的概述 import java.util.*; import java.text.*; public class Task implements Runnable { long n ...
- Java 线程第三版 第八章 Thread与Collection Class 读书笔记
JDK1.2引入最有争议性的改变是将集合类默觉得不是Thread安全性的. 一.Collection Class的概述 1. 具有Threadsafe 的Collection Class: j ...
- Java核心技术卷一基础知识-第7章-图形程序设计-读书笔记
第7章 图形程序设计 本章内容: * Swing概述 * 创建框架 * 框架定位 * 在组件中显示信息 * 处理2D图形 * 使用颜色 * 文本使用特殊字体 * 显示图像 本章主要讲述如何编写定义屏幕 ...
- 《Java并发编程实战》第十三章 显示锁 读书笔记
一.Lock与 ReentrantLock Lock 提供一种无条件的.可轮询的.定时的.可中断的锁获取操作,全部加锁和解锁的方法都是显式的. public interface Lock { void ...
- Javascript模式(第四章函数)------读书笔记
一 背景 js函数的两个特点:1 函数是第一类对象(first-class object):2 函数可以提供作用域 1 函数是对象: 1 函数可以在运行时动态创建,还可以在程序执行过程中创建 2 可以 ...
随机推荐
- continue和break区别
break 语句用于跳出循环. continue 用于跳过循环中的一个迭代. 一个迭代,就是一次循环,continue终止本次循环,继续下一次循环: break,循环终止不再循环.
- Python super() 函数
super() 函数是用于调用父类(超类)的一个方法. super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果重定义某个方法,该方法会覆盖父类的同名方法,但有时 ...
- Jenkins的安装及使用(一)
操作环境:Windows7 一.环境准备 1 安装JDK 本文采用jdk-8u111-windows-x64.exe: 安装完成后配置环境变量. 2 配置tomcat 本文采用tomcat8,免安装版 ...
- MPC&MAGIC
MPC: Popularity-based Caching Strategy for Content Centric Networks MPC: most popular content MPC主要思 ...
- C#并行计算 Parallel.Foreach&Parallel.For
Parallel.For(int fromInclude, int toExclude, Action<int> body) 栗子: Parallel.For(0, 10, (i) =&g ...
- poj2049
优先队列广搜,有人说用SPFA,不知道怎么做的 #include <cstdio> #include <queue> #include <cmath> #inclu ...
- jexus配置支持Owin
vi打开配置文件,加一行 OwinMain=xxx.dll ###################### # Web Site: Default ########################### ...
- Smooth Face Tracking with OpenCV
先马克下,回头跑试试:http://synaptitude.me/blog/smooth-face-tracking-using-opencv/ GitHub:https://github.com/S ...
- jenkins安装及环境搭建
Jenkins 是基于Java开发的一种持续集成工具,所以,Jenkins需要Java环境. Jenkins版本是: JAVA版本是: Tomcat版本是: 或者 Jenkins版本是:2.10.2 ...
- Codeforces 486E LIS of Sequence
LIS of Sequence 我们先找出那些肯定不会再LIS里面. 然后我们从前往后扫一次, 当前位置为 i , 看存不存在一个 j 会在lis上并且a[ j ] > a[ i ], 如果满足 ...