簡體   English   中英

使用Thread.sleep()時run()方法出錯

[英]Error in run() method when using Thread.sleep()

我試圖在自己的線程中運行一個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")
        }
    }

}

}

您正在直接創建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();

線程文檔提供了用法示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM