Barbero dormilon souce code en java?
Barbero dormilon souce code en java.
Barbero dormilon souce code en java.
Pregunta de Informática · 1 respuesta · 1 votos · mejor respuesta de Belindinga
En resumen
S SleepingBarber.
S
SleepingBarber.
Java - - - - - - - - - - - - - - - - - - - - - - - - - / / Figure 2 - 36
import Semaphore ;
public class SleepingBarber extends Thread { / / Shared objects / / Number of customers waiting for service
public static Semaphore customers = new Semaphore(0) ; / / Number of barbers waiting for customers
public static Semaphore barbers = new Semaphore(0) ; / / For mutual exclusion
public static Semaphore mutex = new Semaphore(1) ; / / Customers are waiting (not being cut)
public static int waiting = 0 ; / / Chairs for waiting customers
public static final int CHAIRS = 5 ;
class Barber extends Thread {
private int myNumber ; / / Id for the Barber
public Barber(int i) { / / Constructor for the Barber
myNumber = i ;
}
public void run(){ / / What a barber does
while(true) {
customers.
Down() ; / / Go to sleep if no customers
mutex.
Down() ; / / Acquire access to waiting
waiting = waiting - 1 ; / / Decrement count of waiting / / customers
barbers.
Up() ; / / One barber is now ready to cut / / hair
mutex.
Up() ; / / Release waiting
cut_hair() ; / / Noncritical region
}
}
public void cut_hair(){
System.
Out. println("Barber " + myNumber + " is cutting hair") ;
try {
sleep(7500) ;
} catch (InterruptedException ex){ }
}
} / / end of Barber Class
private class Customer extends Thread {
private int myNumber ; / / Id for the Customer
public Customer(int i) { / / Constructor for the Customer
myNumber = i ;
}
public void run(){ / / What a customer does
mutex.
Down() ; / / Acquire access to waiting
if(waiting ‹ CHAIRS){
waiting = waiting + 1 ; / / Increment count of waiting / / customers
customers.
Up() ; / / Wake up barber if needed
mutex.
Up() ; / / Release waiting
barbers.
Down() ; / / Go to sleep if number of free / / barbers is 0
get_haircut() ; / / Noncritical region
} else {
mutex.
Up() ; / / Shop is full do not wait
}
}
public void get_haircut(){
System.
Out. println("Customer " + myNumber + " is getting his hair cut") ;
try {
sleep(10000) ;
} catch (InterruptedException ex){ }
}
} / / end of Customer Class
public static void main(String args[]) {
SleepingBarber holder = new SleepingBarber() ;
holder.
Start() ;
} / / This thread spins off a number of customers
public void run(){
final int BARBERS = 3 ;
Barber aBarber ;
Customer aCustomer ;
for(int i = 0 ; i‹BARBERS ; i + + ) { / / Create the barbers
aBarber = new Barber(i) ; / / Start the threads running
aBarber.
Start() ;
}
int customerNumber = 0 ;
while(true){
aCustomer = new Customer(customerNumber + + ) ; / / Start the customer running
aCustomer.
Start() ; / / Wait a bit and make another customer
try {
sleep(1000) ;
} catch(InterruptedException ex) {} ;
}
} / / end of run method for SleepingBarber
}
End of SleepingBarber.
Java - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SleepingBarberD.
Java - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - / / Figure 2 - 36
import Semaphore ;
public class SleepingBarberD extends Thread { / / Shared objects / / Number of customers waiting for service
public static Semaphore customers = new Semaphore(0) ; / / Number of barbers waiting for customers
public static Semaphore barbers = new Semaphore(0) ; / / For mutual exclusion
public static Semaphore mutex = new Semaphore(1) ; / / Customers are waiting (not being cut)
public static int waiting = 0 ; / / Chairs for waiting customers
public static final int CHAIRS = 5 ;
class Barber extends Thread {
private int myNumber ; / / Id for the Barber
public Barber(int i) { / / Constructor for the Barber
myNumber = i ;
}
public void run(){ / / What a barber does
while(true) {
customers.
Down() ; / / Go to sleep if no customers
mutex.
Down() ; / / Acquire access to waiting
waiting = waiting - 1 ; / / Decrement count of waiting / / customers
barbers.
Up() ; / / One barber is now ready to cut / / hair
mutex.
Up() ; / / Release waiting
cut_hair() ; / / Noncritical region
}
}
public void cut_hair(){
System.
Out. println("Barber " + myNumber + " is cutting hair") ;
try {
sleep(7500) ;
} catch (InterruptedException ex){ }
}
} / / end of Barber Class
private class Customer extends Thread {
private int myNumber ; / / Id for the Customer
public Customer(int i) { / / Constructor for the Customer
myNumber = i ;
}
public void run(){ / / What a customer does
mutex.
Down() ; / / Acquire access to waiting
if(waiting ‹ CHAIRS){
waiting = waiting + 1 ; / / Increment count of waiting / / customers
customers.
Up() ; / / Wake up barber if needed
mutex.
Up() ; / / Release waiting / / added section RFC Walters 9 December 2005
try { / / random sleep
sleep(10000) ; / /
} catch (InterruptedException e) {} / /
try { / / random sleep
sleep(10000) ; / /
} catch (InterruptedException e) {} / /
try { / / random sleep
sleep(10000) ; / /
} catch (InterruptedException e) {} / / / / end of added section RFC Walters 9 December 2005
barbers.
Down() ; / / Go to sleep if number of free / / barbers is 0
get_haircut() ; / / Noncritical region
} else {
mutex.
Up() ; / / Shop is full do not wait
}
}
public void get_haircut(){
System.
Out. println("Customer " + myNumber + " is getting his hair cut") ;
try {
sleep(10000) ;
} catch (InterruptedException ex){ }
}
} / / end of Customer Class
public static void main(String args[]) {
SleepingBarberD holder = new SleepingBarberD() ;
holder.
Start() ;
} / / This thread spins off a number of customers
public void run(){
final int BARBERS = 3 ;
Barber aBarber ;
Customer aCustomer ;
for(int i = 0 ; i / / Create the barbers
aBarber = new Barber(i) ; / / Start the threads running
aBarber.
Start() ;
}
int customerNumber = 0 ;
while(true){
aCustomer = new Customer(customerNumber + + ) ; / / Start the customer running
aCustomer.
Start() ; / / Wait a bit and make another customer
try {
sleep(1000) ;
} catch(InterruptedException ex) {} ;
}
} / / end of run method for SleepingBarber
}
End of SleepingBarberD.
Java.
S SleepingBarber.
Respuesta : 1. - NetBeans Inicialmente desarrollado por Sun y ahora en manos de Oracle, NetBeans es uno de los IDE para desarrollo Java más completos. NetBeans tiene una estructura modular fácilmente ampliable mediante…
Código#include #include #include #define TRUE 1#define FALSE 0#define CHAIRS 3 / / Sillas Disponibles#define CANT_CUST 5 / / Cantidad de Clientes#define T_CUST 0 / / Tiempo de llegada de Clientes#define T_CORTE 3 / /…