java bool to int_帮助将int转换为bool条件

|

我已经意识到使用整数的一些逻辑

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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值