【发布时间】:2015-05-06 17:08:42
【问题描述】:
我编写了简单的控制台 Java 程序。它使用ExecutorService,并且运行的线程很少。
我在 Windows 和 Linux 下使用它。
在 Linux 下我可以用 CTRL+C 来终止它,但它在 Windows 下不起作用。
我可以在我的程序中以某种方式“修复”这个问题吗? (无需更改操作系统配置或 Java 运行时配置)。
我正在使用 JDK 1.8 / JRE 1.8。我的代码(删除了“工作”部分):
public class FileProcessor {
public static void main(String[] args)
{
// run 5 threads
ExecutorService executor = Executors.newFixedThreadPool(5);
int i;
// get first and last file ID to process
int start = Integer.parseInt(args[0]);
int end = Integer.parseInt(args[1]);
for (i = start; i < end; i++)
{
final int finalId = i; // final necessary in anonymous class
executor.submit(new Runnable()
{
public void run()
{
processFile(finalId);
}
});
}
// I put System.exit(0); here - program will end but threads will still work?
}
public static void processFile(int id)
{
//doing work here
}
}
【问题讨论】:
-
有问题的代码的sn-p会很好