Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Thread.sleep()时run()方法出错_Java_Multithreading_Runtime Error_Runnable_Interrupted Exception - Fatal编程技术网

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

Java 使用Thread.sleep()时run()方法出错,java,multithreading,runtime-error,runnable,interrupted-exception,Java,Multithreading,Runtime Error,Runnable,Interrupted Exception,我正在尝试在自己的线程中运行一个电梯实例。正在调用run()方法,但我的代码中一定存在阻止它运行的错误。我的代码编译和运行时没有错误,但“电梯启动”从未打印 我认为这与Thread.sleep()需要抛出InterruptedException有关。我试图尝试/捕获异常,因为Runnable不会抛出InterruptedException 你能帮我弄清楚它为什么不运行吗 下面是我的run()方法: 下面是电梯类中的start()方法。对于大楼中的每台电梯,这是从主控室调用的 public voi

我正在尝试在自己的线程中运行一个电梯实例。正在调用run()方法,但我的代码中一定存在阻止它运行的错误。我的代码编译和运行时没有错误,但“电梯启动”从未打印

我认为这与Thread.sleep()需要抛出InterruptedException有关。我试图尝试/捕获异常,因为Runnable不会抛出InterruptedException

你能帮我弄清楚它为什么不运行吗

下面是我的run()方法:

下面是电梯类中的start()方法。对于大楼中的每台电梯,这是从主控室调用的

public void start() {
    activeThread = new Thread();
    activeThread.start();
}
moveUp()方法:

moveDown()方法:

完整的passengerevator.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")
        }
    }

}

}

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

public void start() {
    activeThread = new Thread();
    activeThread.start();
}
你需要启动一个线程来运行你的代码。让您的电梯类扩展
线程
或实现
可运行

扩展
线程时

thread = new Elevator();
thread.start();
实施
可运行时

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

提供了使用示例。

你是说它不可编译吗?@Kon,它可编译,但我的电脑上从来没有“电梯启动”功能console@DavidGrinberg它运行,但我的控制台上从来没有“电梯启动”过。您从来没有运行run()方法。您启动了一个新线程,但没有对其进行任何调度。@clenard它运行自己的run()方法,该方法默认为空。要么让升运器延伸螺纹,然后执行新升运器()。或者电梯扩展Runnable并执行新线程(new电梯())。这解决了我的问题。不幸的是,这意味着我找错了方向。哈哈。学习的痛苦。
public void start() {
    activeThread = new Thread();
    activeThread.start();
}
thread = new Elevator();
thread.start();
thread = new Thread(new Elevator());
thread.start();