使用 Jackson 编写 JSON 会阻止我的 TimerTask

发布于 2024-11-04 22:15:21 字数 1202 浏览 1 评论 0原文

我想每 5 秒写一些 JSON。我正在使用 Jackson 编写 JSON,但它似乎阻止了我的 TimerTask。如果我不编写 JSON,TimerTask 每 5 秒运行一次,但是当我尝试编写 JSON 时,它会被阻止并且仅运行一次。我该如何解决这个问题?

public class MyTimerTask extends TimerTask {

    public static void main(String[] args) {

        Timer timer = new Timer();

        // execute MyTimerTask every 5th second
        timer.scheduleAtFixedRate(new MyTimerTask(), 1000L, 5 * 1000L);
    }

    @Override
    public void run() {
        System.out.println("timertask");

        // Write JSON to System.out
        ObjectMapper mapper = new ObjectMapper();
        try {
            mapper.writeValue(System.out, "Hello");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
}

这是我的计时器线程的堆栈转储:

"Timer-0" prio=6 tid=0x02488000 nid=0x10ec in Object.wait() [0x04a6f000]
   java.lang.Thread.State: TIMED_WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x24577fa8> (a java.util.TaskQueue)
        at java.util.TimerThread.mainLoop(Unknown Source)
        - locked <0x24577fa8> (a java.util.TaskQueue)
        at java.util.TimerThread.run(Unknown Source)

I would like to write some JSON every 5th second. I'm using Jackson to write the JSON, but it seem to block my TimerTask. If I don't write JSON, the TimerTask is run every 5th second, but when I try to write JSON it's blocked and only run once. How can I fix this?

public class MyTimerTask extends TimerTask {

    public static void main(String[] args) {

        Timer timer = new Timer();

        // execute MyTimerTask every 5th second
        timer.scheduleAtFixedRate(new MyTimerTask(), 1000L, 5 * 1000L);
    }

    @Override
    public void run() {
        System.out.println("timertask");

        // Write JSON to System.out
        ObjectMapper mapper = new ObjectMapper();
        try {
            mapper.writeValue(System.out, "Hello");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
}

Here is my stack dump for the Timer thread:

"Timer-0" prio=6 tid=0x02488000 nid=0x10ec in Object.wait() [0x04a6f000]
   java.lang.Thread.State: TIMED_WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x24577fa8> (a java.util.TaskQueue)
        at java.util.TimerThread.mainLoop(Unknown Source)
        - locked <0x24577fa8> (a java.util.TaskQueue)
        at java.util.TimerThread.run(Unknown Source)

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

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

发布评论

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

评论(2

遇到 2024-11-11 22:15:21

问题在于您对 System.out 的使用,而不是 Jackson。

The problem is with your use of System.out, not with Jackson.

软甜啾 2024-11-11 22:15:21

它是否被阻止,或者只是抛出一个您没有捕获的异常,这实际上取消了您的任务?

另一方面,“printStackTrace()”几乎从来都不是一种有用的异常处理形式。要么不捕获异常,要么有意义地记录它。仅仅调用“printStackTrace()”就是要求隐藏错误。

如果您的任务确实被阻止,那么下一步就是获取挂起程序的堆栈转储。这应该会告诉您是什么导致代码阻塞。

is it blocked, or is it just throwing an exception which you aren't catching, which effectively cancels your task?

as a side not "printStackTrace()" is almost never a useful form of exception handling. either don't catch the exception or log it meaningfully. just calling "printStackTrace()" is asking to hide bugs.

if your task really is blocked, then the next step is to get a stack dump of your hung program. this should show you what is causing the code to block.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文