gpt4 book ai didi

java - 如何使用同步来订购我的 LinkedBlockingQueue?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:17:56 27 4
gpt4 key购买 nike

我的问题涉及使用同步来订购我用于电梯程序的 LinkedBlockingQueue。

在该程序中,电梯应该尽可能高效地运行:从一楼到十楼的电梯将在上升过程中响应额外的停靠,即使这些停靠是在最初的 10 楼之后进行的称呼。

例如:电梯在一楼,在大楼的 10 楼接到电话。在它上到十楼的过程中,一个在上楼的七楼的人打了个电话。电梯停在7楼,继续到10楼原站。

我的问题是,我如何同步我的线程以有效地响应用户的调用?我不确定我在哪里或如何插入同步。

下面是我的代码:它有一个工作电梯和线程,但电梯不会有效地响应调用,而是在发送每个不同的调用时。为了表示旅行时间,我让电梯“hibernate ”了 3 秒。

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);
}

}
}
}

任何帮助将不胜感激。

最佳答案

我会做不同的,这是基本的想法:

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);
}
}

测试

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

输出

Moving to 2
Stopped
Moving to 3
Moving to 4
Stopped
Moving to 3
Moving to 2
Moving to 1
Stopped

关于java - 如何使用同步来订购我的 LinkedBlockingQueue?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16930789/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com