当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Error in run() method when using Thread.sleep()

问题描述:

I am trying to run an Elevator instance in its own thread. The run() method is being called, but there must be some error in my code that is preventing it from running. My code compiles and runs without errors, but the "Elevator starting up" is never printed.

I think it has to do with Thread.sleep() needing to throw an InterruptedException. I tried to try/catch the exception since Runnable doesn't throw an InterruptedException.

Can you help me figure out why it isn't running?

Here is my run() method:

@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")

}

}

}

Here is my start() method within the Elevator class. This is called from main for each elevator in the building.

public void start() {

activeThread = new Thread();

activeThread.start();

}

The moveUp() method:

public void moveUp() throws InterruptedException {

Thread.sleep(travelSpeed);

setCurrentFloor(currentFloor++);

}

The moveDown() method:

public void moveDown() throws InterruptedException{

Thread.sleep(travelSpeed);

setCurrentFloor(currentFloor--);

}

Full PassengerElevator.class code

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")

}

}

}

}

网友答案:

You are creating a Thread instance directly, and since it is the plain Java Thread class, it has no code in the run method. That means that when you start it, it does nothing. This is the relevant code:

public void start() {
    activeThread = new Thread();
    activeThread.start();
}

You need to start a thread that is going to run your code. Either make your elevator class extend Thread or implement Runnable.

When extending Thread:

thread = new Elevator();
thread.start();

When implementing Runnable:

thread = new Thread(new Elevator());
thread.start();

The Thread documentation provides examples of usage.

分享给朋友:
您可能感兴趣的文章:
随机阅读: