0

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.

Shay
  • 33
  • 1
  • 5
  • Take a look at BlockingQueus/Deques https://docs.oracle.com/javase/7/docs/api/index.html?java/util/concurrent/BlockingQueue.html. I think also java.util.concurrent.locks.Lock with a Condition could also fit in Your solution. – kaos Nov 04 '17 at 17:08
  • Or maybe ThreadPool with bounded queue if You want to work on a thread level https://stackoverflow.com/questions/6306132/java-thread-pool-with-a-bounded-queue. – kaos Nov 04 '17 at 17:10

1 Answers1

0

use this kind of mechanism.use this mechanism in ur project in suitable way .initially add all available parking slots to queue

 public static BlockingQueue blockingQueue = new ArrayBlockingQueue<Integer>(10);

public static void main(String[] args) {
    Thread t1 = new Thread(()-> {
        while(true){
            Random random = new Random();
            try {
                int i = random.nextInt(10);
                if(i!=8) { // a car request parking space
                    System.out.println("waiting until parking available");
                    int j = (int) blockingQueue.take();
                    System.out.println("released parking space "+j+" to requested car " );
                }
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    });
    Thread t2 = new Thread(()->{
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        while(true){
            Random random = new Random();
            try {
                int i = random.nextInt(10);
                if(i!=5) { //car went out from parking space .add avilable slot to queue
                    System.out.println("add  free parking space:" + i +" to queue");
                    blockingQueue.put(i);
                }
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });

    t1.start();
    t2.start();

}
Sameera
  • 85
  • 5