如何避免线程中断异常 [英] How to avoid Thread Interrupted Exception

查看:298
本文介绍了如何避免线程中断异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带线程的Apache lucene为pdf文件编制索引.一些线程花费的时间超过15分钟.线程执行15分钟后,将引发线程中断异常.是否可以通过增加时间限制来避免此问题.

I'm indexing pdf files using Apache lucene with threads. Some threads take time more than 15 minutes. After 15 minutes of thread execution it will throw Thread interrupted Exception.Is there a way to increase the time limit to avoid this issue.

当有一个线程正在运行并且它索引了将近76%的pdf文件时,出现了此异常. 应用程序服务器是Glassfish

I got this exception when there is a single thread running and it indexed nearly 76% of its pdf files. application server is Glassfish

    List<Thread> threads = new ArrayList<Thread>();
    Thread worker;
    for (int a = 1;a <= count; a++) {

        IndexManualRunnable indexManualRunnable =
                new IndexManualRunnable(indexfileLocation, collections, manual, processId);
        worker = new Thread(indexManualRunnable);

        worker.setName(manual.getName());
        worker.setPriority(Thread.MAX_PRIORITY);
        worker.start();

        threads.add(worker);
    }

        for (Thread thread : threads) {
            try {
                thread.join();
            } catch (InterruptedException interruptedException) {

                saveReport("", "", "Interrupted Exception", 1, processId, Category.INDEXING, thread.getName());
                interruptedException.printStackTrace();
            }
        }

推荐答案

更新:

我看到您正在使用Glassfish,并且说此中断是每15分钟发生一次. Glassfish似乎设置为在900秒左右的超时时间,默认情况下为15分钟-这会引发InterruptException.

I see that you are using Glassfish and are saying this interrupt is occurring every time at 15 minutes. It appears Glassfish is set to timeout at around 900 seconds which is 15 minutes by default - this throws an InterruptException.

由于您的应用程序可能需要处理超过15分钟的时间,因此将以下服务器配置更新为您认为合适的时间限制.

Since your application viably needs to process for longer than 15 minutes, update the following server config to a time limit you see fit.

http.request-timeout-seconds

这是一个示例asadmin命令,用于更新属性,但我尚未对其进行测试:

Here is an example asadmin command to update the property but I have not tested it:

 # asadmin set server-config.network-config.protocols.protocol.<listener-name>.http.request-timeout-seconds=-1

- NOTE: <listener-name> is the name of the listener they wish to disable the  timeout on.

 - (-1) means unlimited

要处理中断的线程,您可以自己捕获并处理异常.如果您希望线程继续执行而不管您在理论上什么都不做-但为了保持代码的清洁和正确,我将按以下方式实现它:

To deal with an interrupted thread you can catch and handle the exception yourself. If you want the thread to continue executing regardless you would theoretically do nothing - but to keep the code clean and proper I would implement it as below:

boolean interrupted = false;
try {
    while (true) {
        try {
            return queue.take();
        } catch (InterruptedException e) {
            interrupted = true;
            // fall through and retry
        }
    }
} finally {
    if (interrupted)
        Thread.currentThread().interrupt();
}

我喜欢这个示例,因为它不仅保留了一个空的catch子句,而且保留了以布尔值调用它的事实.这样做是为了,当它最终完成时,您可以检查它是否被中断,是否尊重它并中断当前线程.

I like this example because it does not just leave an empty catch clause, it instead preserves the fact that it was called in a boolean. It does this so that when it finally does complete you can check if it was interrupted and if so respect it and interrupt the current thread.

有关更多信息以及示例来自何处,请查看以下文章: http://www.ibm.com/developerworks/library/j-jtp05236/

For more information and where the example came from look into the following article: http://www.ibm.com/developerworks/library/j-jtp05236/

这篇关于如何避免线程中断异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆