如果想解决上一条引入的问题,就需要控制多个线程按照一定的顺序轮流执行,此时需要让线程间进行通信。在 Object 类中提供了 wait ()、notify ()、notifyAll()方法用于解决线程间的通信问题,由于 Java 中所有类都是 Object 类的子类或间接子类,因此任何类的实例对象都可以直接使用这些方法。
唤醒线程的方法:
方法声明 | 功能描述 |
---|---|
void wait () | 使当前线程放弃同步锁并进入等待,直到其他线程进人此同步锁,并调用 notify()方法,或 notify All()方法唤醒该线程为止 |
void notify() | 唤醒此同步锁上等待的第一个调用 wait ()方法的线程 |
void notifyAll | 唤醒此同步锁上调用 wait ()方法的所有线程 |
其中:
- wait()方法用于使当前线程进入等待状态。
- notify()和 notify AlI()方法用于唤醒当前处于等待状态的线程。
需要注意的是:
wait ()、notify()和 notify All ()这三个方法的调用者都应该是同步锁对象,如果这三个方法的调用者不是同步锁对象,Java虚拟机就会抛出 IllegalMonitorStateException异常。
使用 wait ()和 notify()方法,案例 Storage:
public class Storage2 {
//数据存储数组
private int[] cells = new int[10];
//inPort 表示存入时数组下标,outPos表示取出时数组下标
private int inPos,outPos;
private int count; //存入或者取出数据的数量
//定义一个 put()方法向数组中存入数据
public synchronized void put(int num) {
try {
//如果放入数据等于 cells 的长度,此线程等待
while (count == cells.length) {
this.wait();
}
cells[inPos] = num;
System.out.println("在 cells["+inPos+"]中放入数据---"+cells[inPos]);
inPos++; //存完元素让位置加1
if (inPos == cells.length) {
inPos = 0; //当 inPos为数组长度时,将其置为0
}
count++; //每放一个数据 count加 1
this.notify();
}catch (Exception e){
e.printStackTrace();
}
}
//定义一个 get()方法从数组中取出数据
public synchronized void get() {
try {
while (count == 0) { //如果 count为0,此线程等待
this.wait();
}
int data = cells[outPos];
System.out.println("从 cells["+outPos+"]中取出数据"+data);
outPos++; //取完元素让位置加1
if (outPos == cells.length)
outPos = 0;
count--; //每取出一个元素 count减 1
this.notify();
}catch (Exception e){
e.printStackTrace();
}
}
}
通过使用 synchronized 关键字将 put()方法和 get ()方法修饰为同步方法,之后每操作一次数据,便调用一次 notify ()方法唤醒对应同步锁锁上等待的线程。
当存人数据时,如果 count 的值与 cells 数组的长度相同,说明数组已经添满,此时就需要调用同步锁的 wait ()方法使存入数据的线程进入等待状态。同理,当取出数据时如果 count 的值为0,说明数组已被取空,此时就需要调用同步锁的 wait ()方法,使取出数据的线程进入等待状态。
从运行结果可以看出,存人的数据和取出的数据都是依次递增的自然数。