Java线程:线程的交互
SCJP5学习笔记
线程交互是比较复杂的问题,SCJP要求不很基础:给定一个场景,编写代码来恰当使用等待、通知和通知全部线程。
1、线程交互的基础知识
SCJP所要求的线程交互知识点须要从
java.lang.Object的类的三个方法来学习:
void notify()
唤醒在此对象监视器上等待的单个线程。
void notifyAll()
唤醒在此对象监视器上等待的全部线程。
void wait()
致使当前的线程等待,直到其余线程调用此对象的 notify() 方法或 notifyAll() 方法。
固然,wait()还有另外两个重载方法:
void wait(long timeout)
致使当前的线程等待,直到其余线程调用此对象的 notify() 方法或 notifyAll() 方法,或者超过指定的时间量。
void wait(long timeout, int nanos)
致使当前的线程等待,直到其余线程调用此对象的 notify() 方法或 notifyAll() 方法,或者其余某个线程中断当前线程,或者已超过某个实际时间量。
以上这些方法是帮助线程传递线程关心的时间状态。
关于等待/通知,要记住的关键点是:
必须从同步环境内调用wait()、notify()、notifyAll()方法。线程不能调用对象上等待或通知的方法,除非它拥有那个对象的锁。
wait()、notify()、notifyAll()都是Object的实例方法。与每一个对象具备锁同样,每一个对象能够有一个线程列表,他们等待来自该信号(通知)。线程经过执行对象上的wait()方法得到这个等待列表。从那时候起,它再也不执行任何其余指令,直到调用对象的notify()方法为止。若是多个线程在同一个对象上等待,则将只选择一个线程(不保证以何种顺序)继续执行。若是没有线程等待,则不采起任何特殊操做。
下面看个例子就明白了:
/**
* 计算输出其余线程锁计算的数据
*
* @author leizhimin 2008-9-15 13:20:38
*/
public
class ThreadA {
public
static
void main(String[] args) {
ThreadB b =
new ThreadB();
//启动计算线程
b.start();
//线程A拥有b对象上的锁。线程为了调用wait()或notify()方法,该线程必须是那个对象锁的拥有者
synchronized (b) {
try {
System.out.println(
"等待对象b完成计算。。。");
//当前线程A等待
b.wait();
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(
"b对象计算的总和是:" + b.total);
}
}
}
/**
* 计算1+2+3 ... +100的和
*
* @author leizhimin 2008-9-15 13:20:49
*/
public
class ThreadB
extends Thread {
int total;
public
void run() {
synchronized (
this) {
for (
int i = 0; i < 101; i++) {
total += i;
}
//(完成计算了)唤醒在此对象监视器上等待的单个线程,在本例中线程A被唤醒
notify();
}
}
}
等待对象b完成计算。。。
b对象计算的总和是:5050
Process finished with exit code 0
千万注意:
当在对象上调用wait()方法时,执行该代码的线程当即放弃它在对象上的锁。然而调用notify()时,并不意味着这时线程会放弃其锁。若是线程荣然在完成同步代码,则线程在移出以前不会放弃锁。所以,只要调用notify()并不意味着这时该锁变得可用。
2、多个线程在等待一个对象锁时候使用notifyAll()
在多数状况下,最好通知等待某个对象的全部线程。若是这样作,能够在对象上使用notifyAll()让全部在此对象上等待的线程冲出等待区,返回到可运行状态。
下面给个例子:
/**
* 计算线程
*
* @author leizhimin 2008-9-20 11:15:46
*/
public
class Calculator
extends Thread {
int total;
public
void run() {
synchronized (
this) {
for (
int i = 0; i < 101; i++) {
total += i;
}
}
//通知全部在此对象上等待的线程
notifyAll();
}
}
/**
* 获取计算结果并输出
*
* @author leizhimin 2008-9-20 11:15:22
*/
public
class ReaderResult
extends Thread {
Calculator c;
public ReaderResult(Calculator c) {
this.c = c;
}
public
void run() {
synchronized (c) {
try {
System.out.println(Thread.currentThread() +
"等待计算结果。。。");
c.wait();
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread() +
"计算结果为:" + c.total);
}
}
public
static
void main(String[] args) {
Calculator calculator =
new Calculator();
//启动三个线程,分别获取计算结果
new ReaderResult(calculator).start();
new ReaderResult(calculator).start();
new ReaderResult(calculator).start();
//启动计算线程
calculator.start();
}
}
运行结果:
Thread[Thread-1,5,main]等待计算结果。。。
Thread[Thread-2,5,main]等待计算结果。。。
Thread[Thread-3,5,main]等待计算结果。。。
Exception in thread
"Thread-0" java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.notifyAll(Native Method)
at threadtest.Calculator.run(Calculator.java:18)
Thread[Thread-1,5,main]计算结果为:5050
Thread[Thread-2,5,main]计算结果为:5050
Thread[Thread-3,5,main]计算结果为:5050
Process finished with exit code 0
运行结果代表,程序中有异常,而且屡次运行结果可能有多种输出结果。这就是说明,这个多线程的交互程序还存在问题。到底是出了什么问题,须要深刻的分析和思考,下面将作具体分析。
实际上,上面这个代码中,咱们指望的是读取结果的线程在计算线程调用notifyAll()以前等待便可。 可是,若是计算线程先执行,并在读取结果线程等待以前调用了notify()方法,那么又会发生什么呢?这种状况是可能发生的。由于没法保证线程的不一样部分将按照什么顺序来执行。幸运的是当读取线程运行时,它只能立刻进入等待状态----它没有作任何事情来检查等待的事件是否已经发生。 ----所以,若是计算线程已经调用了notifyAll()方法,那么它就不会再次调用notifyAll(),----而且等待的读取线程将永远保持等待。这固然是开发者所不肯意看到的问题。
所以,当等待的事件发生时,须要可以检查notifyAll()通知事件是否已经发生。
一般,解决上面问题的最佳方式是将