package hr.test;
//临界区,用于控制打印线程的工作顺序和互斥操作
class Mutex{
private int order=1; //记录线程顺序号
private int maxNum=0; //最大线程数
Mutex(int num){
this.maxNum=num;
}
//循环设置下一次要打印的顺序号
public void setOrder(){
this.order=(++order)<=maxNum?order:1;
}
//得到顺序号
public int getOrder(){
return this.order;
}
}
//打印工作
class PrintRunnable implements Runnable{
private Object[] cont=null; //打印内容
private int order=-1; //当前线程的打印顺序
private Mutex mutex=null; //临界区对象
PrintRunnable(Object[] c,int o,Mutex m){
this.cont=c;
this.order=o;
this.mutex=m;
}
public void run(){
try{
//循环打印内容
for(Object c:cont){
//互斥操作临界区
synchronized(mutex){
//如果当前工作线程的顺序号不等于临界区的顺序号,则工作线程等待阻塞,并放弃锁
while(mutex.getOrder()!=this.order)
mutex.wait();
//打印内容
System.out.println(c);
//设置下一次允许打印的线程顺序
mutex.setOrder();
//唤醒在临界区等待的所有线程
mutex.notifyAll();
}
}
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
//测试
public class Test {
public static void main(String[] args){
Integer[] number={1,2,3,4};
Character[] upperChar={'A','B','C','D'};
Character[] lowerChar={'a','b','c','d'};
Mutex mutex=new Mutex(3);
new Thread(new PrintRunnable(number,1,mutex)).start();
new Thread(new PrintRunnable(upperChar,2,mutex)).start();
new Thread(new PrintRunnable(lowerChar,3,mutex)).start();
}
}