使用Thread.sleep()时run()方法出错

时间:2015-02-10 15:43:46

标签: java multithreading runtime-error runnable interrupted-exception

我正在尝试在自己的线程中运行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")
        }
    }

}

}

1 个答案:

答案 0 :(得分:3)

您正在直接创建Thread实例,因为它是普通的Java Thread类,所以run方法中没有代码。这意味着当你启动它时,它什么都不做。这是相关的代码:

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

您需要启动一个将运行您的代码的线程。使您的电梯课程延长Thread或实施Runnable

扩展Thread时:

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

实施Runnable时:

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

Thread documentation提供了使用示例。