使用 Thread.sleep() 时 运行() 方法出错
Error in run() method when using Thread.sleep()
我正在尝试 运行 一个 Elevator 实例在它自己的线程中。正在调用 运行() 方法,但我的代码中一定有一些错误阻止它 运行ning。我的代码编译并且 运行s 没有错误,但是 "Elevator starting up" 从未被打印出来。
我认为这与需要抛出 InterruptedException 的 Thread.sleep() 有关。我尝试 try/catch 异常,因为 Runnable 不会抛出 InterruptedException。
你能帮我弄清楚为什么它不是 运行ning 吗?
这是我的 运行() 方法:
@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 class 中的 start() 方法。这是从大楼中的每部电梯的 main 调用的。
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")
}
}
}
}
您正在直接创建一个 Thread
实例,因为它是普通的 Java Thread
class,它在 run
中没有代码方法。这意味着当您启动它时,它什么都不做。这是相关代码:
public void start() {
activeThread = new Thread();
activeThread.start();
}
您需要启动一个线程来 运行 您的代码。使您的电梯 class 扩展 Thread
或实施 Runnable
.
扩展时Thread
:
thread = new Elevator();
thread.start();
实施时Runnable
:
thread = new Thread(new Elevator());
thread.start();
Thread documentation 提供了使用示例。
我正在尝试 运行 一个 Elevator 实例在它自己的线程中。正在调用 运行() 方法,但我的代码中一定有一些错误阻止它 运行ning。我的代码编译并且 运行s 没有错误,但是 "Elevator starting up" 从未被打印出来。
我认为这与需要抛出 InterruptedException 的 Thread.sleep() 有关。我尝试 try/catch 异常,因为 Runnable 不会抛出 InterruptedException。
你能帮我弄清楚为什么它不是 运行ning 吗?
这是我的 运行() 方法:
@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 class 中的 start() 方法。这是从大楼中的每部电梯的 main 调用的。
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")
}
}
}
}
您正在直接创建一个 Thread
实例,因为它是普通的 Java Thread
class,它在 run
中没有代码方法。这意味着当您启动它时,它什么都不做。这是相关代码:
public void start() {
activeThread = new Thread();
activeThread.start();
}
您需要启动一个线程来 运行 您的代码。使您的电梯 class 扩展 Thread
或实施 Runnable
.
扩展时Thread
:
thread = new Elevator();
thread.start();
实施时Runnable
:
thread = new Thread(new Elevator());
thread.start();
Thread documentation 提供了使用示例。