我正在做一个项目,它要求我们采用休眠的理发器Java代码Here并将其重新制作成酒店。有一些规则,但我具有3对similiar那些麻烦:从并发的Java信号量中去除sleep()
- 线程可以不使用睡眠作为协调
- 忙等待(轮询)的手段是不允许
- 每个线程应该只打印自己的活动
如何去消除睡眠方法,以及什么我代替他们?
我想出了这些信号灯:
import java.util.concurrent.Semaphore;
import java.util.*;
public class Hotel extends Thread {
public static Semaphore deskClerk = new Semaphore(0, false); //semaphore representing the deskClerk
public static Semaphore guestList = new Semaphore(0); //semaphore representing the guests.
public static Semaphore bellHop = new Semaphore(0, false); //semaphore representing the bellHop.
public static Semaphore accessSeats = new Semaphore(1); //the seats that the guestList sit down on upon entering the hotel
int roomCounter = 0; // supposed to be an incrementing global variable integer that increments with each customer acquisition by the front desk employee
int guestID, bellhopID, clerkID;
// the number of chairs in this hotel is 15.
public static final int CHAIRS = 15;
public static int numberOfFreeSeats = CHAIRS;
而且这些类:
游客
class Guest extends Thread {
// we create the integer iD which is a unique ID number for every guest and a boolean notServed which is used in the Guest waiting loop
int iD;
boolean notServed = true;
int baggage;
// Guest Constructor
public Guest(int name, Hotel h) {
iD = name;
guestID = iD;
Random rn = new Random();
baggage = rn.nextInt(6);
//need a random number from 0-5. Represents number of bags.
}
public int getBaggage() {
return baggage;
}
public boolean checkBaggage() {
if(this.getBaggage() > 2) {
return true;
}
else {
return false;
}
}
public void run() {
while (notServed) { // as long as the customer has not been served
try {
accessSeats.acquire(); // tries to get access to the hotel chairs
if (numberOfFreeSeats > 0) { // if there are any free seats
System.out.println("Guest " + this.iD + " has entered hotel with " + this.getBaggage() + " bags");
numberOfFreeSeats--; // sitting down on a chair
guestList.release(); // notify the front desk salesmen that there is a customer
accessSeats.release(); // don't need to lock the chairs
// anymore
try {
roomCounter++;
deskClerk.acquire(); // now it's this guestList turn
// but we have to wait if the deskClerk is busy
notServed = false; // the guest will now leave after they are served.
this.get_Served(); // Serving the guests
if (this.checkBaggage() == true){
bellHop.release();
System.out.println("Guest " + this.iD + " requests help with bags");
//from here, need a way for the bellhop to signal the guest that they should tip the bellhop, receive the bags, and retire.
}
else {
System.out.println("Guest " + this.iD + " enters room #" + roomCounter + " and retires for the evening");
}
}
catch (InterruptedException ex) {
}
}
else { // there are no free seats
System.out.println("There are no free seats. Guest " + this.iD + " has left the hotel.");
accessSeats.release(); // release the lock on the seats
notServed = false; // the customer will leave since there are no spots in the queue left.
}
} catch (InterruptedException ex) {
}
}
}
/* this method simulates a front desk employee processing a guest */
public void get_Served() {
System.out.println("Guest " + this.iD
+ " receives room key for room " + roomCounter + " from front desk employee " + clerkID);
try {
deskClerk.release();
}
finally{
}
}
书记
/* THE CLERK THREAD */
class Clerk extends Thread {
boolean busy = false;
public Clerk(int name, boolean bus) {
clerkID = name;
busy = bus;
}
// this method will simulate booking rooms
public void serve() {
System.out.println("Front desk employee " + this.getId() + " registers guest " + guestID + " and assigns room " + roomCounter);
try {
sleep(3000);
} catch (InterruptedException ex) {
}
}
public void run() {
while (true) { // runs in an infinite loop
try {
guestList.acquire(); // tries to acquire a customer - if none is available he goes to sleep
accessSeats.acquire(); // at this time he has been awaken -> want to modify the number of available seats
numberOfFreeSeats++; // one chair gets free
deskClerk.release(); // the clerk is ready to serve the customer
accessSeats.release(); // we don't need the lock on the hotel chairs anymore
this.serve(); // serving the customer
}
catch (InterruptedException ex) {
}
}
}
}
侍者
/*THE BELLHOP THREAD */
class bellHop extends Thread {
boolean busy = false;
public bellHop(int name, boolean f) {
bellhopID = name;
busy = f;
}
public void handleBaggage() {
if(bellHop.availablePermits() != 0){
System.out.println("Bellhop " + this.getId() + " receives bags from guest " + guestID);
System.out.println("Bellhop " + this.getId() + " delivers bags to guest " + guestID);
try {
bellHop.acquire();
}
catch (InterruptedException ex){
System.out.println(this.getId() + " Cannot handle baggage");
}
}
}
public void run() {
while (true) { // runs in an infinite loop
try {
this.handleBaggage(); // take baggage up to guests room and receive a tip
sleep(100);
}
catch (InterruptedException ex) {
}
}
}
}
主要&酒店的run()
// main method
public static void main(String args[]) {
System.out.println("Simulation Starts");
Hotel hotel = new Hotel(); // Creates a new hotel
Clerk george = hotel.new Clerk(1, false); System.out.println("Front desk employee " + george.getId() + " created");
Clerk greg = hotel.new Clerk(2, false); System.out.println("Front desk employee " + greg.getId() + " created");
bellHop bill = hotel.new bellHop(1, false); System.out.println("Bellhop " + bill.getId() + " created");
bellHop bob = hotel.new bellHop(2, false); System.out.println("Bellhop " + bob.getId() + " created");
hotel.start(); // begins the simulation
greg.start();
george.start();
bill.start();
bob.start();
}
public void run() {
Hotel h = new Hotel();
// This method creates 25 new guests
for (int i = 1; i < 10; i++) {
Guest aGuest = new Guest(i, h);
aGuest.start();
try {
sleep(3000);
}
catch (InterruptedException ex) {
}
}
}
}
我投票结束这个问题作为题外话,因为它是作业。 – Isaac
@Isaac:作业题目不会自动脱离主题。请参阅[我如何问及回答作业问题](http:// meta。在meta上的stackexchange.com/questions/10811/how-do-i-ask-and-answer-homework-questions)。 –