|
我已经意识到使用整数的一些逻辑
import java.util.concurrent.locks.*;
public class MassageSalon implements Salon{
//I want to transform this into bool
private int customerOnCouch = 0;
private int customerPaid = 0;
private int masseurAvailable = 0;
private int masseurBusy = 0;
private int masseurDone = 0;
private int masseurClose = 0;
Lock lock = new ReentrantLock();
Condition sleep = lock.newCondition();
public void getNextCustomer() throws InterruptedException{
lock.lock();
masseurAvailable++;
System.out.println(\"masseur is available to handle a new customer\");
sleep.signalAll();
//wait for next customer
while(customerOnCouch < masseurAvailable){
System.out.println(\"masseur sleeps\");
sleep.await();
System.out.println(\"masseur wakes up\");
}
//customer takes couch, masseur busy
masseurBusy++;
System.out.println(\"masseur busy\");
lock.unlock();
}
public void finishedMassage() throws InterruptedException{
lock.lock();
//eventually the masseur finishes the massage
System.out.println(\"eventually the masseur finishes the massage\");
masseurDone++;
sleep.signalAll();
//and closes the deal as soon as the customer paid
while(masseurDone > customerPaid){
System.out.println(\"wait for payment\");
sleep.await();
}
System.out.println(\"and closes the deal as soon as the customer paid\");
masseurClose++;
lock.unlock();
}
public void getMassage(int customerId) throws InterruptedException{
lock.lock();
while(customerOnCouch > masseurClose){
System.out.println(customerId + \" on couch waiting\");
sleep.await();
}
//takes couch
customerOnCouch++;
System.out.println(customerId + \" on couch\");
sleep.signalAll();
while(masseurDone <= customerPaid){
System.out.println(\"customer \" + customerId + \" sleeps\");
sleep.await();
}
System.out.println(\"customer \" + customerId + \" wakes up\");
customerPaid++;
System.out.println(\"customer \" + customerId + \" payes\");
sleep.signalAll();
lock.unlock();
}
}
此逻辑使用整数。由于这是一个线程上下文,因此我现在想使用布尔值而不是整数来避免溢出。
所以我想到了这个。但是,这与上面的示例不同。什么是正确的转型?
import java.util.concurrent.locks.*;
public class MassageSalon implements Salon{
private boolean customerOnCouch = false;
private boolean customerPaid = false;
private boolean masseurAvailable = false;
private boolean masseurBusy = false;
private boolean masseurDone = false;
private boolean masseurClose = false;
Lock lock = new ReentrantLock();
Condition sleep = lock.newCondition();
public void getNextCustomer() throws InterruptedException{
lock.lock();
masseurAvailable = true;
customerOnCouch = false;
masseurBusy = false;
customerPaid = false;
masseurDone = false;
masseurClose = false;
System.out.println(\"masseur is available to handle a new customer\");
sleep.signalAll();
//wait for next customer
while(!customerOnCouch && masseurAvailable){
System.out.println(\"masseur sleeps\");
sleep.await();
System.out.println(\"masseur wakes up\");
}
//neuer kunde nimmt platz, masseur ist busy
masseurBusy = true;
System.out.println(\"masseur busy\");
lock.unlock();
}
public void finishedMassage() throws InterruptedException{
lock.lock();
//eventually the masseur finishes the massage
System.out.println(\"eventually the masseur finishes the massage\");
masseurDone = true;
sleep.signalAll();
//and closes the deal as soon as the customer paid
while(masseurDone && !customerPaid){
System.out.println(\"wait for payment\");
sleep.await();
}
System.out.println(\"and closes the deal as soon as the customer paid\");
masseurClose = true;
lock.unlock();
}
public void getMassage(int customerId) throws InterruptedException{
lock.lock();
while(customerOnCouch && !masseurClose){
System.out.println(customerId + \" on couch waiting\");
sleep.await();
}
//takes couch
customerOnCouch = true;
System.out.println(customerId + \" on couch\");
sleep.signalAll();
while(masseurDone && !customerPaid){
System.out.println(\"customer \" + customerId + \" sleeps\");
sleep.await();
}
System.out.println(\"customer \" + customerId + \" wakes up\");
customerPaid = true;
System.out.println(\"customer \" + customerId + \" payes\");
sleep.signalAll();
lock.unlock();
}
}