Java 定时器的问题

发布于 2024-10-08 20:07:12 字数 852 浏览 0 评论 0原文

我在计时器方面遇到了一个奇怪的问题...在 NetBeans 中测试时,我的计时器工作正常,但是一旦我直接从终端(Ubuntu 10.4)编译并运行,应该每分钟发生的任务就会执行一次,并且永远不会执行再次执行...

这是我的代码:

public static void main(String[] args) throws SQLException
{
    // schedule db update task to occur every 15 mins
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask ()
        {
            @Override public void run()
            {
                doUpdate();
            }
        } , 0, updateInterval * 1000 * 60 );
        System.out.print("Starting auto update @ ");

    // schedule cpu usage check to occur every 1 min
    Timer cpu = new Timer();
    cpu.scheduleAtFixedRate(new TimerTask ()
        {
            @Override public void run()
            {
                getCPU();
            }
        } , 0, cpuUpdateInterval * 1000 * 60 );
}

我做错了什么吗?

I am having a weird problem with timers... My Timer works correctly when testing in NetBeans, but as soon as I compile and run directly from the terminal (Ubuntu 10.4), the task that is supposed to occur every minute executes once and never executes again...

Here is my code:

public static void main(String[] args) throws SQLException
{
    // schedule db update task to occur every 15 mins
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask ()
        {
            @Override public void run()
            {
                doUpdate();
            }
        } , 0, updateInterval * 1000 * 60 );
        System.out.print("Starting auto update @ ");

    // schedule cpu usage check to occur every 1 min
    Timer cpu = new Timer();
    cpu.scheduleAtFixedRate(new TimerTask ()
        {
            @Override public void run()
            {
                getCPU();
            }
        } , 0, cpuUpdateInterval * 1000 * 60 );
}

Is there something that I am doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少钕鈤記 2024-10-15 20:07:12

getCPU() 是做什么的?如果它正在运行 JNA/JNI 代码或外部库,则可能您的命令行设置中缺少该库。

What does getCPU() do? If it is running JNA/JNI code or an external library, maybe you are missing the library from your command line settings.

时光无声 2024-10-15 20:07:12

通过这段代码,

import java.util.Timer;
import java.util.TimerTask;

/** @see http://stackoverflow.com/questions/4503829 */
public class TimerTest {

    private static final int MAX = 8;

    public static void main(String[] args) {
        final Timer cpu = new Timer();
        cpu.scheduleAtFixedRate(new TimerTask() {

            private int count;

            @Override
            public void run() {
                count++;
                System.out.println("Count: " + count);
                if (count == MAX) {
                    cpu.cancel();
                }
            }
        }, 100, 1000);
    }
}

我得到了预期的结果:

$ make run
java -version
java version "1.6.0_20"
OpenJDK Runtime Environment (IcedTea6 1.9.2) (6b20-1.9.2-0ubuntu1~10.04.1)
OpenJDK Client VM (build 19.0-b09, mixed mode, sharing)
java TimerTest
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
Count: 6
Count: 7
Count: 8

With this code,

import java.util.Timer;
import java.util.TimerTask;

/** @see http://stackoverflow.com/questions/4503829 */
public class TimerTest {

    private static final int MAX = 8;

    public static void main(String[] args) {
        final Timer cpu = new Timer();
        cpu.scheduleAtFixedRate(new TimerTask() {

            private int count;

            @Override
            public void run() {
                count++;
                System.out.println("Count: " + count);
                if (count == MAX) {
                    cpu.cancel();
                }
            }
        }, 100, 1000);
    }
}

I got the expected results:

$ make run
java -version
java version "1.6.0_20"
OpenJDK Runtime Environment (IcedTea6 1.9.2) (6b20-1.9.2-0ubuntu1~10.04.1)
OpenJDK Client VM (build 19.0-b09, mixed mode, sharing)
java TimerTest
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
Count: 6
Count: 7
Count: 8
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文