【发布时间】:2015-02-10 15:43:45
【问题描述】:
我正在尝试在其自己的线程中运行 Elevator 实例。正在调用 run() 方法,但我的代码中一定有一些错误导致它无法运行。我的代码编译并运行没有错误,但从未打印过“电梯启动”。
我认为这与需要抛出 InterruptedException 的 Thread.sleep() 有关。我试图尝试/捕获异常,因为 Runnable 不会引发 InterruptedException。
你能帮我弄清楚为什么它没有运行吗?
这是我的 run() 方法:
@Override
public void run() {
System.out.printf("Elevator starting up");
while(true) {
try {
if (currentDirection == Direction.UP) {
this.moveUp();
}
else if (currentDirection == Direction.DOWN) {
this.moveDown();
}
else {Thread.sleep(250);}
}
catch (InterruptedException ie) {
System.out.println("Elevator has experienced a critical error")
}
}
}
这是我在 Elevator 类中的 start() 方法。这是建筑物中每个电梯的主调用。
public void start() {
activeThread = new Thread();
activeThread.start();
}
moveUp() 方法:
public void moveUp() throws InterruptedException {
Thread.sleep(travelSpeed);
setCurrentFloor(currentFloor++);
}
moveDown() 方法:
public void moveDown() throws InterruptedException{
Thread.sleep(travelSpeed);
setCurrentFloor(currentFloor--);
}
完整的PassengerElevator.class代码
public class PassengerElevator implements ElevatorMover, Runnable {
private final int elevID; // elevator number
private final int maxCapacity; // max capacity of the elevator
private int currentCapacity; // the current capacity of the elevator
private final long travelSpeed; // length of travel time between floors
private final long doorSpeed; // length of time door stays open
private int currentFloor; // the current floor the elevator is on
private final int defaultFloor; // the default floor after timeout
private Direction currentDirection; // the current direction the elevator is moving
public Thread activeThread = null; // contains the instance of an elevator thread
/**
* Constructor
* @param elevID the ID number, as an int, given to the elevator
*/
public PassengerElevator(int elevID) {
this.elevID = elevID;
maxCapacity = 10;
currentCapacity = 0;
travelSpeed = 500; // in milliseconds
doorSpeed = 500; // in milliseconds
currentFloor = 1;
defaultFloor = 1;
currentDirection = Direction.IDLE;
}
/**
* makes the elevator go up one floor. Takes travelSpeed time
* @throws InterruptedException
*/
@Override
public void moveUp() throws InterruptedException {
Thread.sleep(travelSpeed);
setCurrentFloor(currentFloor++);
}
/**
* makes the elevator go down one floor. Takes travelSpeed time
* @throws InterruptedException
*/
@Override
public void moveDown() throws InterruptedException{
Thread.sleep(travelSpeed);
setCurrentFloor(currentFloor--);
}
/**
* makes the elevator door open for doorSpeed time. When door is open people
* move into elevator
* @throws InterruptedException
*/
@Override
public void openDoors() throws InterruptedException{
Thread.sleep(doorSpeed);
}
public int getElevID() {
return elevID;
}
private int getMaxCapacity() {
return maxCapacity;
}
private int getCurrentCapacity() {
return currentCapacity;
}
private void setCurrentCapacity(int x) {
currentCapacity = x;
}
private double getTravelSpeed() {
return travelSpeed;
}
private double getDoorSpeed() {
return doorSpeed;
}
public int getCurrentFloor() {
return currentFloor;
}
private void setCurrentFloor(int x) {
currentFloor = x;
}
private int getDefaultFloor() {
return defaultFloor;
}
private void setCurrentDirection(Direction x) {
currentDirection = x;
}
private Direction getCurrentDirection() {
return currentDirection;
}
/**
* Starts a new thread for an elevator instance to run in
*/
public void start() {
activeThread = new Thread();
activeThread.start();
}
/**
* The running loop for an elevator instance. Client will change current direction
* and use the currentFloor as a check.
*/
@Override
public void run() {
System.out.printf("Elevator starting up");
while(true) {
try {
if (currentDirection == Direction.UP) {
this.moveUp();
}
else if (currentDirection == Direction.DOWN) {
this.moveDown();
}
else {Thread.sleep(250);}
}
catch (InterruptedException ie) {
System.out.println("Elevator has experienced a critical error")
}
}
}
}
【问题讨论】:
-
你是说这不能编译?
-
@Kon,它可以编译,但我的控制台上从来没有出现“电梯启动”
-
@DavidGrinberg 它运行,但我的控制台上从来没有出现“电梯启动”
-
你永远不会运行你的 run() 方法。您启动了一个新线程,但没有在其上安排任何内容。
-
@clenard 它运行自己的 run() 方法,默认为空。要么让 Elevator 扩展 Thread,然后执行 new Elevator()。或者 Elevator 扩展 Runnable 并执行 new Thread(new Elevator())。
标签: java multithreading runtime-error runnable interrupted-exception