简体   繁体   English

Java Swing:事件调度线程VS秒表

[英]Java Swing: Event-Dispatching Thread VS Stopwatch

I have a java program with a GUI based on Swing. 我有一个带有基于Swing的GUI的Java程序。 In the main class there are some computations which are grouped in a separate thread. 在主类中,一些计算被分组在单独的线程中。 My problem is : I need to start a stopwatch when those computations begin, show the timer WHILE THEY PROCEEDING, stop as they finish. 我的问题是:这些计算开始时,我需要启动秒表,在进行过程时显示计时器,在完成时停止。 If I only needed to count the time left I would start the stopwatch in the separate thread do all computations in the main thread and when they finished stopped the stopwatch thread and display the timer. 如果只需要计算剩余时间,我将在单独的线程中启动秒表,在主线程中执行所有计算,并在它们停止时停止秒表线程并显示计时器。 As I know java swing is singlethreaded. 据我所知,java swing是单线程的。 So that I can repaint the digiths of the stopwatch in the main swing 这样我就可以在主挥杆上重新粉刷秒表的数字

The easiest thing to do would be to use a javax.swing.Timer. 最简单的方法是使用javax.swing.Timer。 It's easy to have a Timer send periodic update events to update the GUI, and it's easy to stop or reset the timer as needed. 让计时器发送定期的更新事件以更新GUI很容易,并且可以根据需要停止或重置计时器。

I've read that SwingWorker seems to be what I need. 我读过SwingWorker似乎正是我所需要的。

I'll endorse @Ernest Friedman-Hill's javax.swing.Timer suggestion, but this SwingWorker example is worth exploring. 我会支持@Ernest Friedman-Hill的javax.swing.Timer建议,但是这个SwingWorker 示例值得探讨。 It may help you leverage your existing thread code. 它可以帮助您利用现有的线程代码。

First of all, I want to say thank you to everybody. 首先,我想对大家说谢谢。

The problem was solved by usind SwingWorker in such a way: the main swing window I developed as a child of SwingWorker . usind SwingWorker通过以下方式解决了该问题:我是SwingWorker的孩子开发的主摆动窗口。 All the computations were in doInBackground method. 所有的计算都在doInBackground方法中。 While they were proceeding, a timer (which is in a separate thread) was invoked in SwingUtilities.invokeLater. 在它们进行时,在SwingUtilities.invokeLater中调用了一个计时器(在单独的线程中)。 In the done method when all the computations finished the timer was stopped. 完成方法中,当所有计算完成时,计时器停止运行。

The Swing timer will more or less do the job, but it can be somewhat inaccurate. Swing计时器或多或少会完成这项工作,但是可能有些不准确。 In fact, the inaccuracy can be noticeable. 实际上,这种不准确性可能很明显。 For instance, if you make your timer with the Swing Timer and then watch the numbers as they tick by, you might notice it isn't always exactly a second in between ticks. 例如,如果您使用Swing计时器制作计时器,然后看着数字滴答作响,您可能会注意到,滴答之间的间隔并不总是一秒钟。 In other words the resolution of the timer isn't that great. 换句话说,计时器的分辨率不是很好。

An alternative to the Swing Timer is System.nanoTime() . Swing计时器的替代方法是System.nanoTime() I always use this. 我总是用这个。 It has much better resolution. 它具有更好的分辨率。 It returns the current system time in nano seconds as a long. 它以毫秒为单位返回当前系统时间。

Here is a quick example of how you could use this to count by the seconds: 这是一个简单的示例,说明如何使用它来按秒计数:

long previousTime = 0L;
int seconds = 0;
while(true)
{
    long currTime = System.nanoTime();

    if((currTime - previousTime) / 1000000000L >= 1L)
    {
        previousTime = currTime;
        seconds++;
        System.out.println("Seconds: " seconds)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM