Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4617844
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:10:56+00:00 2026-05-22T02:10:56+00:00

I have realized some logic using integers import java.util.concurrent.locks.*; public class MassageSalon implements Salon{

  • 0

I have realized some logic using integers

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

This logic uses integers. Since this is a threaded context I want to use booleans now instead of integers to avoid overflows.

So I came up with this. However this does not the same as the example above. What would be the right transformation?

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();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:10:56+00:00Added an answer on May 22, 2026 at 2:10 am

    I’m not sure that you have a valid “model” using either integers or booleans.

    You are modelling some states, and for each state you use multiple counters or booleans. To illustrate: the masseuse has four booleans which can be set independently, and yet many combinations are meaningless:

     Busy == true and Done == true?
    

    Instead model the state of the masseuse using an enum, with values such as Available and Working.

    Have a separate class for the Masseuse with methods such as

     boolean isAvailable();
     void startWorking();
     void stopWorking();
    

    Your customers, I think you should model in two parts: a single Customer class, which has states such as Waiting, OnCouch, NeedToPay, Complete. and separately a queue to hold the customers in Waiting state.

    In response to your determination to use booleans:

    To make this work I would still encapsulate the state information in Mansseuse and Customer objects. Then I would consider each state transition: for example from working to stopped, for that transition have a method which sets the appropriate booleans. In this way you get a clear picture of the effect of each transition, and presumably will notice that some flags need to be unset in certain situations.

    As a learning point you should consider how brittle your solution is – I predict that should you ever need to add a few more states in the life-cycle your code will be unmaintainable, and probably already is quite hard to understand for anybody other than yourself. You’re finding this hard to reason about because your representation (multiple booleans) doesn’t really fit the problem domain – a set of state transitions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just realized that in some place in my code I have the return
I have a class that has about 20-some methods in it. Each one does
Say we have realized a value of TDD too late. Project is already matured,
I decided to teach myself assembly language. I have realized that my program will
I am creating a large table dynamically using Javascript. I have realised the time
Have just started using Google Chrome , and noticed in parts of our site,
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I have some complex code. Complex, but it was working. The I wanted to
I have some page layouts that require multiple columns and all of their content
I recently realized that I add some form of row creation timestamp and possibly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.