我的问题涉及使用同步来订购我用于电梯程序的LinkedBlockingQueue.
在该程序中,电梯应该表现得尽可能高效:从一楼到十楼的电梯将响应上行中的其他停靠点,即使这些停靠点是在最初的10楼呼叫之后做出的.
例如:电梯在二楼,并在大楼的十楼接到电话.在到达10楼的过程中,7楼的一个人向上打了一个电话.电梯在第7层停下,然后继续在第10层停下.
我的问题是,如何同步线程以有效响应用户的呼叫?我不确定在哪里或如何插入同步.
下面是我的代码:它有一个工作中的电梯和线程,但是电梯不会有效地响应呼叫,而是在发送每个不同的呼叫时响应.为了表示旅行时间,我让电梯“睡眠”了3秒钟.
public class Elevator {
public Thread main;
public Thread thread2;
LinkedBlockingQueue lb = new LinkedBlockingQueue();
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);
}
}
}
}
}
任何帮助都感激不尽.