import java.util.*;
//工厂
class Test{
public void run(){
Queens queen=new Queens();
QueenThread qt=new QueenThread(queen);
new Thread(qt).start();
new Thread(qt).start();
System.out.println("Len= "+queen.getQueens().size());
}
public static void main(String[]args){
new Test().run();
}
}
//生产缓存
class Queens{
private List buffList=null;
private final List queenList=new ArrayList();
public Queens(){
init();
}
public void init(){
int i=0;
for(int len=0;len<2;len++){
buffList=new ArrayList();
for(;i<(len+1)*3;i++){
buffList.add(String.valueOf(i+1));
}
queenList.add(buffList);
}
}
public synchronized void deleteQueen(int index){
queenList.remove(index);
}
public List getQueens(){
return queenList;
}
public int getLen(){
return queenList.size();
}
}
//处理队列缓存
class QueenThread implements Runnable{
private static int id=0;
private Queens queen;
public QueenThread(Queens queen){
this.queen=queen;
}
private ThreadLocal tl=new ThreadLocal(){
protected Integer initialValue(){
return new Integer(id++);
}
};
public void run(){
int currentId=((Integer)(tl.get())).intValue();
List queenList=(List)queen.getQueens().get(currentId);
for(int i=0;i<queenList.size();i++){
System.out.println(Thread.currentThread().getName()+" "+(String)queenList.get(i));
try{
Thread.sleep(2000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+" ID= "+currentId);
}
}