I have created a networked car park system that contains two entrance clients, two exit clients, and one server. My aim is to get cars to queue up at an entrance if there is no space available in the car park.
This is what I have done so far:
import javax.swing.JFrame;
import javax.swing.JOptionPane;
@SuppressWarnings("unused")
public class SharedState extends Thread{
private SharedState mySharedObj;
private String myThreadName;
private int totalSpaces;
private int groundSpaces;
private int firstSpaces;
private boolean accessing=false; // true a thread has a lock, false otherwise
private int threadsWaiting = 0; // number of waiting writers
JFrame Notification = new JFrame();
// Constructor
SharedState(int groundFloor, int firstFloor) {
groundSpaces = groundFloor;
firstSpaces = firstFloor;
}
//Attempt to aquire a lock
public synchronized void acquireLock() throws InterruptedException{
Thread me = Thread.currentThread(); // get a ref to the current thread
++threadsWaiting;
while (accessing) { // while someone else is accessing or threadsWaiting > 0
//wait for the lock to be released - see releaseLock() below
wait();
}
// nobody has got a lock so get one
--threadsWaiting;
accessing = true;
}
// Releases a lock to when a thread is finished
public synchronized void releaseLock() {
//release the lock and tell everyone
accessing = false;
notifyAll();
Thread me = Thread.currentThread(); // get a ref to the current thread
}
public synchronized String processInput(String myThreadName, String theInput) throws InterruptedException
{
String theOutput = null;
// Check what the client said
if (theInput != null)
{
//Correct request
if(myThreadName.equals("GroundFloorEntrance"))
{
if(groundSpaces > 0)
{
groundSpaces--;
totalSpaces = groundSpaces + firstSpaces;
theOutput = "Vehicle arrived.\nGround floor spaces = " + groundSpaces + "\nTotal spaces = " + totalSpaces;
}
else if (firstSpaces > 0)
{
firstSpaces--;
totalSpaces = groundSpaces + firstSpaces;
theOutput = "Vehicle arrived.\nFirst floor spaces = " + firstSpaces + "\nTotal spaces = " + totalSpaces;
}
else
{
theOutput = "No spaces available, please queue.";
}
}
if(myThreadName.equals("FirstFloorEntrance"))
{
if(firstSpaces > 0)
{
firstSpaces--;
totalSpaces = groundSpaces + firstSpaces;
theOutput = "Vehicle arrived.\nFirst floor spaces = " + firstSpaces + "\nTotal spaces = " + totalSpaces;
}
else if (groundSpaces > 0)
{
groundSpaces--;
totalSpaces = groundSpaces + firstSpaces;
theOutput = "Vehicle arrived.\nGround floor spaces = " + groundSpaces + "\nTotal spaces = " + totalSpaces;
}
else
{
theOutput = "No spaces available, please queue.";
}
}
if(myThreadName.equals("GroundFloorExit1"))
{
if(groundSpaces < 20)
{
groundSpaces++;
totalSpaces = groundSpaces + firstSpaces;
theOutput = "Vehicle departed.\nGround floor spaces = " + groundSpaces + "\nTotal spaces = " + totalSpaces;
}
else
{
theOutput = "Vehicle departed.\nGround floor spaces = " + groundSpaces + "\nTotal spaces = " + totalSpaces;
}
}
if(myThreadName.equals("GroundFloorExit2"))
{
if(firstSpaces < 20)
{
firstSpaces++;
totalSpaces = groundSpaces + firstSpaces;
theOutput = "Vehicle departed.\nFirst floor spaces = " + firstSpaces + "\nTotal spaces = " + totalSpaces;
}
else
{
theOutput = "Vehicle departed.\nFirst floor spaces = " + firstSpaces + "\nTotal spaces = " + totalSpaces;
}
}
}
//Return the output message to the Server
JOptionPane.showMessageDialog(Notification, theOutput);
System.out.println("\n" + theOutput);
return theOutput;
}
}
I would like for the cars to queue when there is no space available and automatically run when a car exits. I believe I would need to effectively queue the threads to run within an IF statement when one of the exit clients operates. However, I am not sure how I would do this.