저는 이틀 동안이 문제를 해결하기 위해 노력해 왔습니다. 마침내 포기합니다. 제가 동료 인 사람이 제가 누락 된 것을 제공 할 수 있기를 희망하며 코드를 게시하고 있습니다. 왜냐하면 내가 가까이에 있다고 생각하기 때문입니다. , 도와주세요! 이발사를 들어잠자는 이발사, 세마포어?
코드 : 고객에 대한
public Semaphore customer = new Semaphore(0);
public Semaphore barber = new Semaphore(0);
public Semaphore accessSeats = new Semaphore(1);
public int freeChairs = 5;
@Override
public void run() {
while (true) {
try {
customer.acquire(); //it seems the barber never acuires a customer, even after the semaphore is released
accessSeats.acquire();
freeChairs += 1;
cutHair();
barber.release();
accessSeats.release();
} catch (InterruptedException ex){}
코드 :
public void run() {
while (notCut) {
try {
bs.accessSeats.acquire();
} catch (InterruptedException ex) {}
if (bs.freeChairs > 0) {
bs.freeChairs -= 1;
bs.customer.release();
bs.accessSeats.release();
try {
bs.barber.acquire(); //the customer cant get past this line
getHaircut();
} catch (InterruptedException ex) {}
} else {
bs.accessSeats.release();
notCut = false;
}
}
}
나는 임의의 타이머 1 이발사 100 개 고객을 만들 수 있습니다.
편집 : cutHair()
이발사를 1-5 초 동안 자고 있습니다 (무작위). getHaircut()
은 단지 메시지를 출력하고 세트를 설정합니다. notCut=false;
예외가 표시됩니까? 당신이 예외를 잡는 것 같아. 최소한 스택 추적을 인쇄하십시오. –
코드를 읽기 쉽게 만들고 싶었 기 때문에 인쇄했지만 제거했습니다. –
cutHair() 및 getHaircut() 메서드는 무엇을합니까? InterruptedException을 throw하는 것이 있습니까? –