java - 文件写入程序随机退出,无错误

标签 java file-io scheduled-tasks

目前我正在尝试使用 Java 的文件类写入 txt 文件。此任务应每 5 秒完成一次,并由 ScheduledExecutorService 进行调度。一段时间以来,它正常工作,但在随机时间之后,我的程序退出,没有任何警告、错误等。我试图重现这一点,但它似乎是随机发生的。我也尝试过使用 PrintWriter,但这会随机导致相同的行为。

提前致谢!!!

这是我的代码:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class FileWritingClass
{
    public static void main(String[] args) throws IOException
    {
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

        scheduler.scheduleAtFixedRate(
            new Runnable() {
                long i;
                String str = "";
                Path path = Paths.get("TestFile.txt");

                @Override
                public void run() {

                    str = LocalTime.now().toString() + ": still alive after " + i + " times.";
                    i++;

                    try
                    {   
                        System.out.println(str);
                        Files.write(path, (str + System.lineSeparator()).getBytes(), StandardOpenOption.APPEND);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            },
            500 /* Startdelay */,
            5000 /* period */,
            TimeUnit.MILLISECONDS );        
    }
}

最佳答案

Executor正在吞咽 IOException 以外的异常。捕获Throwable而不仅仅是IOException .

Executor当遇到 IOException 以外的异常时失败。 VM 仍处于 Activity 状态,但 Executor失败了。

这里有一些可以尝试的代码:

                    try
                    {   
                        System.out.println(str);
                        Files.write(path, (str + System.lineSeparator()).getBytes(), StandardOpenOption.APPEND);
                        if(new Random().nextBoolean()) {
                            System.out.println("Here we go.");
                            throw new RuntimeException("Uncaught");
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch(Throwable t) {
                        t.printStackTrace();
                        System.out.println("The show must go on...");
                    }

请注意我如何在写入后给出 50/50 的机会抛出未捕获的异常(只是为了快速失败)。 如果删除第二个 catch ,它就会停止打印。 随着catch ,输出变为:

12:46:00.780250700: still alive after 0 times.
12:46:05.706355500: still alive after 1 times.
Here we go.
java.lang.RuntimeException:
Uncaught at
KeepaTokenRecorder$FileWritingClass$1.run(KeepaTokenRecorder.java:89)
    at [...]
The show must go on...
12:46:15.705899200: still alive after 3 times.

关于java - 文件写入程序随机退出,无错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58375299/

相关文章:

c - 工作队列和中断失败

javascript - 如何在 Node js中设置 Node 计划作业,使其在服务器重新启动后不会销毁

Java 在正常功能上实现调度程序错误

java - 在 netbeans 中找不到符号

java - 我如何获得学生成绩计算器程序中类似成绩字母的总数

python - 如何在 python 中的匹配行之后获取行

java - Java中Scanner的构建和使用

java - 用于容器化 postgresql 的 Spring boot 的正确 jdbc 是什么

java - Android中的视频处理

c - 文件处理 C 程序可以读取的格式