2


Is there a stopwatch in Java? にある ようなストップウォッチを実装しようとしています。、しかしとしてThread

SecondsCounterのクラスは を実装しRunnable、そのメソッドを呼び出すstartと、ローカル変数としてスレッドが作成されます。

このスレッドがrunメソッドの最初の行と競合しているように見えます:   this.runningTime = System.currentTimeMillis();...条件while (this.runningTime < this.endTime)が壊れる ことがないため (無限ループ)... したがって、その ' as を実装し、

含むクラスを持つのは正しくありません。良い?Runnable Thread



main メソッドを持つクラスは次のとおりです。

public class MultithreadingTest {

    public static void main(String[] args) {
        int totalSeconds = 5;
        SecondsPrinter printer = new SecondsPrinter(totalSeconds);
        printer.startPrinting();
    }
} // end of class


...秒プリンタークラス:

public class SecondsPrinter {

    // composition here:
    private SecondsCounter clock;

    public SecondsPrinter(int totalSeconds) {
        this.clock = new SecondsCounter(totalSeconds);
    }

    public void startPrinting() {
        this.clock.start();
        while (this.clock.isRunning()) {

            // this is incorrectly always printing the maximum seconds value:
            System.out.println(this.clock.getCurrentSecond());
        }
    }
} // end of class


...そして秒カウンタークラス:

public class SecondsCounter implements Runnable {

    private int totalSeconds, currentSecond;
    private long startTime, runningTime, endTime;
    private Thread thread;

    public SecondsCounter(int totalSeconds) {
        this.totalSeconds = totalSeconds;
    }

    public int getCurrentSecond() {
        return this.currentSecond;
    }

    @Override
    public void run() {
        this.runningTime = System.currentTimeMillis();

        // this is an infinite loop, but it shouldn't be:
        while (this.runningTime < this.endTime) {
            this.currentSecond = (int)(this.endTime - this.runningTime) / 1000;
        }

        // this code is never reached:
        this.stop();
    }

    public void start() {
        this.startTime = System.currentTimeMillis();
        this.runningTime = this.startTime;
        this.endTime = this.startTime + (this.totalSeconds * 1000);

        // multithreading here:
        this.thread = new Thread(this);
        this.thread.start();
    }

    public boolean isRunning() {
        return this.thread.isAlive();
    }

    public void stop() {
        this.thread.interrupt();
        this.thread = null;
    }
} // end of class
4

1 に答える 1

4

実際、それは無限ループのはずです。あなたのループを見てください。

while (this.runningTime < this.endTime) {
    this.currentSecond = (int)(this.endTime - this.runningTime) / 1000;
}

条件は、 while ループすることを示していますrunningTime < endTime。どこでrunningTime更新されますか? 次のようなものをループに追加すると、機能するはずです。

public void run() {
    this.runningTime = System.currentTimeMillis();

    // no longer an infinite loop
    while (this.runningTime < this.endTime) {
        this.currentSecond = (int)(this.endTime - this.runningTime) / 1000;
        this.runningTime = System.currentTimeMillis();
    }

    // this code is now reached.
    this.stop();
}

それを組み合わせることもできます(または変数runningTimeを完全に削除することもできます):

public void run() {
    while ((this.runningTime = System.currentTimeMillis()) < this.endTime) {
        this.currentSecond = (int)(this.endTime - this.runningTime) / 1000;
    }

    this.stop();
}
于 2013-11-09T06:10:11.567 に答える