Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

i need help fixing my code the loop keeps running before i can enter a number please help me. import java.util.Date; import java.util.List; import java.util.Scanner;

i need help fixing my code the loop keeps running before i can enter a number please help me.

import java.util.Date;

import java.util.List;

import java.util.Scanner;

import java.util.concurrent.TimeUnit;

import java.util.LinkedList;

public class SleepingBarberClass {

public static void main(String a[])

{

BarberShop shop = new BarberShop();

Barber barber = new Barber(shop);

TheCustomerGenerator cg = new TheCustomerGenerator(shop);

Thread thbarber = new Thread(barber);

Thread thcg = new Thread(cg);

thcg.start();

thbarber.start();

}

}

class Barber implements Runnable

{

BarberShop shop;

public Barber(BarberShop shop)

{

this.shop = shop;

}

public void run()

{

try

{

Thread.sleep(10000);

}

catch(InterruptedException iex)

{

iex.printStackTrace();

}

System.out.println("Barbar has started..");

while(true)

{

shop.cutTheHair();

}

}

}

class Customer implements Runnable

{

String name;

Date inTime;

BarberShop shop;

public Customer(BarberShop shop)

{

this.shop = shop;

}

public String getName() {

return name;

}

public void setInTime(Date inTime) {

this.inTime = inTime;

}

public Date getInTime() {

return inTime;

}

public void setName(String name) {

this.name = name;

}

public void run()

{

goForTheHairCut();

}

private synchronized void goForTheHairCut()

{

shop.add(this);

}

}

class TheCustomerGenerator implements Runnable

{

BarberShop shop;

public TheCustomerGenerator(BarberShop shop)

{

this.shop = shop;

}

public void run()

{

while(true)

{

Customer customer = new Customer(shop);

customer.setInTime(new Date());

Thread thcustomer = new Thread(customer);

customer.setName("Customer "+thcustomer.getId());

thcustomer.start();

try

{

TimeUnit.SECONDS.sleep((long)(Math.random()*10));

}

catch(InterruptedException iex)

{

iex.printStackTrace();

}

}

}

}

class BarberShop

{

int nchair;

List listCustomer;

public BarberShop()

{

nchair = 3;

listCustomer = new LinkedList();

}

public void add(Customer customer)

{

System.out.println("Customer : "+customer.getName()+ " is entering the shop at "+customer.getInTime());

synchronized (listCustomer)

{

if(listCustomer.size() == nchair)

{

System.out.println("There is no chair available for customer "+customer.getName());

System.out.println("Customer "+customer.getName()+"Exists...");

return ;

}

((LinkedList)listCustomer).offer(customer);

System.out.println("Customer : "+customer.getName()+ " got the chair.");

if(listCustomer.size()==1)

listCustomer.notify();

}

}

public void cutTheHair()

{

Customer customer;

System.out.println("Barber is waiting for the lock.");

synchronized (listCustomer)

{

while(listCustomer.size()==0)

{

System.out.println("Barber waiting for the customer.");

try

{

listCustomer.wait();

}

catch(InterruptedException iex)

{

iex.printStackTrace();

}

}

System.out.println("Barber has found customer in chair.");

customer = (Customer)((LinkedList)listCustomer).poll();

}

long duration=0;

try

{

System.out.println("Cutting hair of the Customer : "+customer.getName());

duration = (long)(Math.random()*10);

TimeUnit.SECONDS.sleep(duration);

}

catch(InterruptedException iex)

{

iex.printStackTrace();

}

Scanner reader = new Scanner(System.in);

System.out.println("Enter a number: ");

int n = reader.nextInt();

System.out.println("Completed Cutting hair of the Customer : "+customer.getName() + " in only " +duration+ " seconds.");

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

Students also viewed these Databases questions