我们把系统中使用某类资源的线程称为消费者,产生或释放同类资源的线程称为生产者。
在下面的Java的应用程序中,生产者线程向文件中写数据,消费者从文件中读数据,这样,在这个程序中同时运行的两个线程共享同一个文件资源。通过这个例子我们来了解怎样使它们同步。
--观看动画--
例6.5
class SyncStack{ //同步堆栈类
private int index = 0; //堆栈指针初始值为0
private char []buffer = new char[6]; //堆栈有6个字符的空间
public synchronized void push(char c){ //加上互斥锁
while(index = = buffer.length){ //堆栈已满,不能压栈
try{
this.wait(); //等待,直到有数据出栈
}catch(InterruptedException e){}
}
this.notify(); //通知其它线程把数据出栈
buffer[index] = c; //数据入栈
index++; //指针向上移动
}
public synchronized char pop(){ //加上互斥锁
while(index ==0){ //堆栈无数据,不能出栈
try{
this.wait(); //等待其它线程把数据入栈
}catch(InterruptedException e){}
}
this.notify(); //通知其它线程入栈
index- -; //指针向下移动
return buffer[index]; //数据出栈
}
}
class Producer implements Runnable{ //生产者类
SyncStack theStack;
//生产者类生成的字母都保存到同步堆栈中
public Producer(SyncStack s){
theStack = s;
}
public void run(){
char c;
for(int i=0; i<20; i++){
c =(char)(Math.random()*26+'A');
//随机产生20个字符
theStack.push(c); //把字符入栈
System.out.println("Produced: "+c); //打印字符
try{
Thread.sleep((int)(Math.random()*1000));
/*每产生一个字符线程就睡眠*/
}catch(InterruptedException e){}
}
}
}
class Consumer implements Runnable{ //消费者类
SyncStack theStack;
//消费者类获得的字符都来自同步堆栈
public Consumer(SyncStack s){
theStack = s;
}
public void run(){
char c;
for(int i=0;i<20;i++){
c = theStack.pop(); //从堆栈中读取字符
System.out.println("Consumed: "+c);
//打印字符
try{
Thread.sleep((int)(Math.random()*1000));
/*每读取一个字符线程就睡眠*/
}catch(InterruptedException e){}
}
}
}
public class SyncTest{
public static void main(String args[]){
SyncStack stack = new SyncStack();
//下面的消费者类对象和生产者类对象所操作的是同一个同步堆栈对象
Runnable source=new Producer(stack);
Runnable sink = new Consumer(stack);
Thread t1 = new Thread(source); //线程实例化
Thread t2 = new Thread(sink); //线程实例化
t1.start(); //线程启动
t2.start(); //线程启动
}
}
类Producer是生产者模型,其中的 run()方法中定义了生产者线程所做的操作,循环调用push()方法,将生产的20个字母送入堆栈中,每次执行完push操作后,调用sleep()方法睡眠一段随机时间,以给其他线程执行的机会。类Consumer是消费者模型,循环调用pop()方法,从堆栈中取出一个数据,一共取20次,每次执行完pop操作后,调用sleep()方法睡眠一段随机时间,以给其他线程执行的机会。
程序执行结果
Produced:V
Consumed:V
Produced:E
Consumed:E
Produced:P
Produced:L
...
Consumed:L
Consumed:P
在上述的例子中,通过运用wait()和notify()方法来实现线程的同步,在同步中还会用到notifyAll()方法,一般来说,每个共享对象的互斥锁存在两个队列,一个是锁等待队列,另一个是锁申请队列,锁申请队列中的第一个线程可以对该共享对象进行操作,而锁等待队列中的线程在某些情况下将移入到锁申请队列。下面比较一下wait()、notify()和notifyAll()方法:
(1) wait,nofity,notifyAll必须在已经持有锁的情况下执行,所以它们只能出现在synchronized作用的范围内,也就是出现在用
synchronized修饰的方法或类中。
(2) wait的作用:释放已持有的锁,进入等待队列.
(3) notify的作用:唤醒wait队列中的第一个线程并把它移入锁申请队列.
(4) notifyAll的作用:唤醒wait队列中的所有的线程并把它们移入锁申请队列.
注意:
1) suspend()和resume()
在JDK1.2中不再使用suspend()和resume(),其相应功能由wait()和notify()来实现。
2) stop()
在JDK1.2中不再使用stop(),而是通过标志位来使程序正常执行完毕。例6.6就是一个典型的例子。
例6.6
public class Xyz implements Runnable {
private boolean timeToQuit=false; //标志位初始值为假
public void run() {
while(!timeToQuit) {//只要标志位为假,线程继续运行
…
}
}
public void stopRunning() {
timeToQuit=true;} //标志位设为真,表示程序正常结束
}
public class ControlThread {
private Runnable r=new Xyz();
private Thread t=new Thread(r);
public void startThread() {
t.start();
}
public void stopThread() {
r.stopRunning(); }
//通过调用stopRunning方法来终止线程运行
}
|