wait和notify是Object类而非Thread类的两个操作。这两个操作只能在线程同步的时候有效,并且只能出现在synchronize()方法synchronize块中。
运用wait与notify模拟实现下载文件的技术要点如下:
(1)将显示文件的线程处于等待状态wait,如果下载线程下载了新的文件,就通报notify显示线程,显示线程再将文件显示出来。
(2)wait方法有两种常用的形式:wait(长整型参数)和wait()。前者是线程等待指定的毫秒数,超过则继续进行,如果参数为0则表示无线等待;wait()方法相当于wait(0),表示无限等待。
(3)notify方法也有两种形式: notify()和notifyAll()。notify()方法只通知一个等待该对象的线程,如果有多个,则根据线程调度选一个;notifyAll通知所有等待该对象的线程。
package core;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
public class TextWaitAndNotify {// 操作wati()和notify()方法的类
private static boolean isDone = false;// 标识线程是否停止
private static List<String> fileList = new ArrayList<String>();// 创建集合列表对象
private DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss SSSS");
private class DownLoadFile extends Thread {// 创建模拟下载文件类
private String[] file = new String[] { "time.txt", "axis.jar", "ajax in action.rar", "post.zip" };
public void run() {
for (int i = 0; i < file.length; i++) {
try {
int time = new Random().nextInt(100) * 5;// 获得随机生成的秒数
System.out.println(dateFormat.format(new Date()) + " 下载文件" + file[i] + "用时 " + time + " 毫秒");
Thread.sleep(time);// 线程休眠相应的time时间
} catch (InterruptedException e) {// 捕获异常
System.out.println("下载文件出错 "+e.getMessage());
}
synchronized (fileList) {//实现对集合列表fileList的同步
System.out.println(dateFormat.format(new Date())+" 下载线程已下载文件"+file[i]);
fileList.add(file[i]);//将文件添加到集合列表中
fileList.notify();//通知所有等待fileList的线程-这边已经添加元素到fileList列表
}
}
//当for循环把所有下载文件放入fileList后便设置标识位为真
isDone=true;//重新设置标识
System.out.println(dateFormat.format(new Date())+"下载线程, 退出");
}
}
private class ShowFile extends Thread{
public void run(){//实现线程类的方法
while(!isDone){
synchronized (fileList) {
try {
fileList.wait(100);//集合列表等待,1秒超时
} catch (InterruptedException e) {// 捕获异常
System.out.println("线程等待出现错误: "+e.getMessage());
}
while(fileList.size()>0){
System.out.println(dateFormat.format(new Date())+"显示线程 显示文件"+fileList.remove(0));
}
}
}
System.out.println(dateFormat.format(new Date())+"显示线程 退出");
}
}
public static void main(String[] args) {
TextWaitAndNotify text=new TextWaitAndNotify();
text.new DownLoadFile().start();
text.new ShowFile().start();
}
}

源程序解读
TextWaitAndNotify类的内部类DownLoadFile用于模拟文件下载。其声明一个字符串数组来模拟存储的文件。run()方法根据字符串数组的长度进行循环获得随机生成的下载秒数并输出下载文件的信息,设置线程的休眠时间为随机生成的下载秒数。运用同步方式将字符串文件添加到集合列表notify()方法通报所有等待集合列表的线程。
ShowFile类的run()方法根据标识为真进行循环