私はJavaの生産者/消費者問題の変種に取り組んでいます。オブジェクトを作成するプロデューサースレッドがあります。オブジェクトは優先度ブロックキューに入れられ、メインコンテナーであるコントローラー(制限付きバッファー)に渡されます。
キューの理由は、メインコンテナにオブジェクトAの特定の割合がある場合、タイプBのオブジェクト、および確認を求められた他のいくつかのシナリオのみを受け入れるためです。コードの何が問題になっているのかを理解するのに問題があります。デバッガーは、InQueueのin.offerとProducerのin.pushからジャンプしています。任意の方向性やアドバイスをいただければ幸いです。
import java.util.concurrent.PriorityBlockingQueue;
public class InQueue implements Runnable {
Controller c;
private PriorityBlockingQueue in;
public InQueue(Controller c) {
this.c = c;
in = new PriorityBlockingQueue();
}
public void push(C c) {
in.offer(c);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run() {
while (true) {
try {
C temp = (C) in.take(); //will block if empty
c.arrive(temp);
} catch (InterruptedException e) {} // TODO
}
}
}
public class Controller {
private BoundedBuffer buffer;
private int used;
Controller(int capacity) {
this.buffer = new BoundedBuffer(capacity);
used = 0;
}
public void arrive(C c) {
try {
buffer.put(c);
used++;
} catch (InterruptedException e) { } //TODO
}
public C depart() {
C temp = null; //BAD IDEA?
try {
temp = (C)buffer.take();
used--;
} catch (InterruptedException e) { } //TODO
return temp; //could be null
}
}