Question

My question involves using synchronization to order my LinkedBlockingQueue that I am using for an elevator program.

In the program, the elevator is supposed to behave as efficiently as possible: an elevator going from the first floor to the tenth floor will respond to additional stops on the way up, even if those stops were made after the original 10th floor call.

For example: the elevator is on the first floor, and gets a call on the 10th floor of the building. During it's travel up to the tenth floor, a person on the 7th floor going UP makes a call. The elevator makes a stop on the 7th floor and continues to original stop on the 10th.

My question is, how do i synchronize my threads to respond efficiently to the user's calls? i'm not sure where or how i insert synchronization.

Below is my code: it has a working elevator and threads, but the elevator will not respond to calls efficiently, but rather as each different call is sent. To represent travel time, I have the elevator "sleep" for 3 seconds.

public class Elevator {
public Thread main;
public Thread thread2;
LinkedBlockingQueue<Integer> lb = new LinkedBlockingQueue<Integer>();
public int num1;

public static void main(String[] args) {
    Elevator ele = new Elevator();
}

public Elevator() {
    main = new Thread(new Task1());
    thread2 = new Thread(new Task2());
    main.start();
    thread2.start();
}

public class Task1 implements Runnable {

    @Override
    public void run() {

        // create loop with whcih the program asks for floors
        // add the scanner int to the arraylist queue

        while (thread2.isAlive()) {
            System.out.println("Choose a Floor: 1-10");
            Scanner s = new Scanner(System.in);
            int floorInput = s.nextInt();

            if (floorInput > 10 || floorInput < 0) {
                System.out.println("Floor does not exist.");
            } else if (floorInput == 0) {
                System.out.println("You have exitted the elevator.");
                System.exit(0);
            } else {

                lb.add(floorInput);
                System.out.println("floor inputed into the queue.");
                // System.out.println(lb);
                // j++;

            }
        }
    }
}

public class Task2 implements Runnable {

    @Override
    public void run() {

        // motor class
        // looks in the queue and goes to the floors

        // while (!lb.isEmpty())
        int i = 0;
        int s = 0;
        while (i < 5) { //endless loop, neccessary for an elevator
            if (lb.contains(2)) {
                try {
                    System.out.println("Travelling to 2nd Floor...");
                    Thread.sleep(3000);
                    s = lb.take();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Arrived on 2nd Floor.");
                System.out.println("removed: " + s);
            } else if (lb.contains(1)) {
                try {
                    System.out.println("Travelling to 1st Floor...");
                    Thread.sleep(3000);
                    s = lb.take();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Arrived on 1st Floor.");
                System.out.println("removed: " + s);
            } else if (lb.contains(3)) {
                try {
                    System.out.println("Travelling to 3rd Floor...");
                    Thread.sleep(3000);
                    s = lb.take();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Arrived on 3rd Floor.");
                System.out.println("removed: " + s);
            } else if (lb.contains(4)) {
                try {
                    System.out.println("Travelling to 4th Floor...");
                    Thread.sleep(3000);
                    s = lb.take();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Arrived on 4th Floor.");
                System.out.println("removed: " + s);
            } else if (lb.contains(5)) {
                try {
                    System.out.println("Travelling to 5th Floor...");
                    Thread.sleep(3000);
                    s = lb.take();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Arrived on 5th Floor.");
                System.out.println("removed: " + s);
            } else if (lb.contains(6)) {

                try {
                    System.out.println("Travelling to 6th Floor...");
                    Thread.sleep(3000);
                    s = lb.take();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Arrived on 6th Floor.");
                System.out.println("removed: " + s);
            } else if (lb.contains(7)) {
                try {
                    System.out.println("Travelling to 7th Floor...");
                    Thread.sleep(3000);
                    s = lb.take();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Arrived on 7th Floor.");
                System.out.println("removed: " + s);
            }

            else if (lb.contains(8)) {
                try {
                    System.out.println("Travelling to 8th Floor...");
                    Thread.sleep(3000);
                    s = lb.take();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Arrived on 8th Floor.");
                System.out.println("removed: " + s);
            }

            else if (lb.contains(9)) {
                try {
                    System.out.println("Travelling to 9th Floor...");
                    Thread.sleep(3000);
                    s = lb.take();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Arrived on 9th Floor.");
                System.out.println("removed: " + s);
            } else if (lb.contains(10)) {
                try {
                    System.out.println("Travelling to 10th Floor...");
                    Thread.sleep(3000);
                    s = lb.take();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Arrived on 10th Floor.");
                System.out.println("removed: " + s);
            }

        }
    }
}

}

Any help will be much appreciated.

Was it helpful?

Solution

I would do it differently, this is the basic idea:

class Elevator extends Thread {
    NavigableSet<Integer> calls = new ConcurrentSkipListSet<>();
    int floor = 1;

    @Override
    public void run() {
        for (;;) {
            try {
                Thread.sleep(100);
                while (calls.higher(floor) != null) {
                    move(+1);
                }
                while (calls.lower(floor) != null) {
                    move(-1);
                }
            } catch (InterruptedException e) {
            }
        }
    }

    void move(int n) throws InterruptedException {
        floor += n;
        System.out.println("Moving to " + floor);
        Thread.sleep(3000);
        if (calls.remove(floor)) {
            System.out.println("Stopped");
            Thread.sleep(3000);
        }
    }

    void call(int floor) {
        calls.add(floor);
    }
}

test

    Elevator e = new Elevator();
    e.start();
    e.call(2);
    e.call(4);
    e.call(1);

output

Moving to 2
Stopped
Moving to 3
Moving to 4
Stopped
Moving to 3
Moving to 2
Moving to 1
Stopped
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top