Java多线程不停止

Java多线程不停止,java,multithreading,Java,Multithreading,我为一种扩展Thread类的“秒表”编写了以下代码: package StopWatch; //Code taken from: //https://stackoverflow.com/questions/9526041/how-to-program-for-a-stopwatch public class Stopwatch extends Thread { private long startTime; private boolean started; publ

我为一种扩展Thread类的“秒表”编写了以下代码:

package StopWatch;

//Code taken from:
//https://stackoverflow.com/questions/9526041/how-to-program-for-a-stopwatch

public class Stopwatch extends Thread {
    private long startTime;
    private boolean started;

    public void startTimer() {
        this.startTime = System.currentTimeMillis();
        this.started = true;
        this.start();
    }

    public void run() {
        while(started){/*currentTimeMillis increases on its own */}
        System.out.println("timer stopped");
    }

    public int[] getTime() {
        long milliTime = System.currentTimeMillis() - this.startTime;
        int[] time = new int[]{0,0,0,0};
        time[0] = (int)(milliTime / 3600000);    //gives number of hours elapsed
        time[1] = (int)(milliTime / 60000) % 60; //gives number of remaining minutes elapsed
        time[2] = (int)(milliTime / 1000) % 60;  //gives number of remaining seconds elapsed
        time[3] = (int)(milliTime);              //gives number of remaining milliseconds elapsed
        return time;
    }

    public void stopTimer() {
        this.started = false;
    }
}
我正在以下驱动程序类中测试它:

import StopWatch.Stopwatch;
public class StopWatchTest {
    public static void main(String[] args) {
        Stopwatch stopwatch = new Stopwatch();

        stopwatch.startTimer();
        int sum = 0;
        for (long i = 0; i < 100000; i++) {
            sum++;
        }
        int[] time = stopwatch.getTime();

        for (int i = 0; i < 4; i++) {
            if (i < 3) {
                System.out.print(time[i]+":");
            } else {
                System.out.print(time[i]);
            }
        }
        stopwatch.stopTimer();
    }
}
导入秒表。秒表;
公共类秒表测试{
公共静态void main(字符串[]args){
秒表秒表=新秒表();
秒表;
整数和=0;
对于(长i=0;i<100000;i++){
sum++;
}
int[]time=stopwatch.getTime();
对于(int i=0;i<4;i++){
如果(i<3){
系统输出打印(时间[i]+“:”);
}否则{
系统输出打印(时间[i]);
}
}
秒表;
}
}
我的意图是使用Stopwatch类的实例来测量各种代码块(例如驱动程序类中的for循环)的性能,方法是让主线程中的这些Stopwatch对象在执行我要评估的代码块之前在单独的线程中启动计时器,然后让它们(Stopwatch对象)一旦主线程中所述块的执行完成,停止计时器。我知道有更简单、更容易的方法可以做到这一点,但我想尝试这样做,作为一种“概念证明”,并简单地使用多线程来提高性能,但我遇到了一些问题:

1) 当我运行驱动程序类StopWatch测试时,每次都会得到看似随机和任意的输出(但大部分是0:0:0)

2) 在我得到0:0:0:0这样的输出之后,主线程(或者可能是秒表线程,我甚至不再确定)似乎从未停止执行

3) 当我尝试使用断点等进行调试时,我会得到完全出乎意料的行为,这取决于我放置断点的位置(主线程有时会完成执行,但随机输出为0:0:13:2112,而其他时候,我只是卡在秒表线程中)


第3点与我的关系不如第1点和第2点,因为当一个或多个线程在断点处暂停以进行调试时,我对多线程的行为知之甚少(我怀疑当我中断主线程时,秒表线程会继续运行)。第1点和第2点让我更加烦恼,因为我不明白它们为什么会发生

要开始,您应该将启动的布尔值标记为volatile:

private volatile boolean started;
这应该是可行的,但它会产生一个繁忙的循环,这对您的CPU使用非常不利。
接下来应该查看
wait()/notify()
方法。

既然
run()
什么都不做,为什么要启动线程?它只会浪费时间和内存。run()只是用来提前一个计时器,至少这是我的意图,(a)“测量不同代码块的性能”您应该使用,(b)“简单地使用多线程来提高性能”您应该使用内置于现代Java中的Executors框架。请参阅。@bmanicus131因为
run()
什么都不做,所以它没有效果,也不能提前任何计时器。如果您指的是System.currentTimeMillis(),它不属于任何线程,也不属于硬件,根本不可能进行干预。这很有帮助!代码现在正在按预期运行。但是,我不确定如何使用wait()和notify()来提高这里的CPU使用率。据我所知,wait()和notify()只能由对象调用,用于暂停/启动“拥有”所述对象的线程。除了主线程中的stopwatch之外,我没有处理任何对象,我在这里如何使用这些对象?您可以将“this”用作监视器对象,因此在while(started)循环中调用this.wait(),在stopTimer()中调用this.notify()。