0
나는 생산자와 소비자 문제를 세마포어로 구현했다. 소비를위한 제품이 없을 때 현재 스레드는 생산자가 제품을 생산할 때까지 까지 기다려야합니다. 나를 안내하십시오.어떻게하면 자바의 세마포어에서 특정 테드를 멈출 수 있습니까?
나는 생산자와 소비자 문제를 세마포어로 구현했다. 소비를위한 제품이 없을 때 현재 스레드는 생산자가 제품을 생산할 때까지 까지 기다려야합니다. 나를 안내하십시오.어떻게하면 자바의 세마포어에서 특정 테드를 멈출 수 있습니까?
Java's BlockingQueue
을 확인하십시오. 이미이 동작을 지원합니다. JavaDoc의에서 가져온
코드를 예로 들어, 위에 링크 :
class Producer implements Runnable {
private final BlockingQueue queue;
Producer(BlockingQueue q) { queue = q; }
public void run() {
try {
while (true) { queue.put(produce()); }
} catch (InterruptedException ex) { ... handle ...}
}
Object produce() { ... }
}
class Consumer implements Runnable {
private final BlockingQueue queue;
Consumer(BlockingQueue q) { queue = q; }
public void run() {
try {
while (true) { consume(queue.take()); }
} catch (InterruptedException ex) { ... handle ...}
}
void consume(Object x) { ... }
}
class Setup {
void main() {
BlockingQueue q = new SomeQueueImplementation();
Producer p = new Producer(q);
Consumer c1 = new Consumer(q);
Consumer c2 = new Consumer(q);
new Thread(p).start();
new Thread(c1).start();
new Thread(c2).start();
}
}
그리고 현재 코드는 무엇인가? 코드 없음, 도움 없음 ... – fge