매우 기본적인 초보자 용 코드가 있습니다. 링크 된 블로킹 큐 내에있는 작업자 개체 풀을 호출합니다. 이 코드는 작업자 개체를 다시 사이클링하여 입력 데이터를 인쇄합니다. ;) (시점에서 현재 = (quoteworkerobject) idlechannels.take -LinkedBlockingQueue의 교착 상태 (?)
public class throttleheapthreadpool{
private quoteworkerobject[] channels;
private LinkedBlockingQueue<quoteworkerobject> idlechannels;
public throttleheapthreadpool(int poolsize,int stocks){
channels=new quoteworkerobject[poolsize];
idlechannels=new LinkedBlockingQueue<quoteworkerobject>();
for(int i=1;i<poolsize;i++){
channels[i]=new quoteworkerobject(idlechannels);
idlechannels.add(channels[i]);//All WORKERS to Idle pool to start
}
}
public void execute(Integer quote){
quoteworkerobject current = null;
try {
//extract worker from pool
current = (quoteworkerobject)idlechannels.take();
current.put(quote);
} catch (InterruptedException e) {
}
}
class quoteworkerobject{
LinkedBlockingQueue<Integer> taskqueue=new LinkedBlockingQueue<Integer>();
Thread quotethread=null;
LinkedBlockingQueue<quoteworkerobject> idle=null;
@SuppressWarnings("unchecked")
public quoteworkerobject(LinkedBlockingQueue<quoteworkerobject> idlechannels){
this.idle=idlechannels;
Runnable r=new Runnable(){
public void run() {
insertquote();
}
};
quotethread=new Thread(r);
quotethread.start();//spawn a thread from the worker
}
public void put(Integer quote){
taskqueue.add(quote);
}
public void insertquote(){
try{
Integer thisquote=taskqueue.take();
idle.add(this);
}
catch(Exception ex){
}
}
}
public static void main(String[] args){
throttleheapthreadpool pool=new throttleheapthreadpool(5,200);
Random randomGenerator = new Random();
for(int node=0;node < 20;node++){
int d=randomGenerator.nextInt(5*200);
pool.execute(d);
}
}
}
지속적 8 실행에 동결이 코드는 :
나는 다음과 같이 동결/일관성 교착 상태를 찾을 수
위의 오류는 무엇입니까?
풀 크기를 5에서 20으로 변경하면 도움이됩니까? – scientiaesthete
@RanadhirNag가 투표를하거나 당신이 원하는 답변을 수락하십시오. ;) – Eugene