Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何避免线程中断异常_Java_Multithreading_Glassfish - Fatal编程技术网

Java 如何避免线程中断异常

Java 如何避免线程中断异常,java,multithreading,glassfish,Java,Multithreading,Glassfish,我正在使用ApacheLucene和线程为pdf文件编制索引。有些线程需要15分钟以上的时间。线程执行15分钟后,它将抛出线程中断异常。是否有方法增加时间限制以避免此问题 当有一个线程运行时,我得到了这个异常,它索引了将近76%的pdf文件。 应用服务器是Glassfish <thread-pools> <thread-pool name="http-thread-pool" idle-thread-timeout-seconds="1800" /&

我正在使用ApacheLucene和线程为pdf文件编制索引。有些线程需要15分钟以上的时间。线程执行15分钟后,它将抛出线程中断异常。是否有方法增加时间限制以避免此问题

当有一个线程运行时,我得到了这个异常,它索引了将近76%的pdf文件。 应用服务器是Glassfish

      <thread-pools>
        <thread-pool name="http-thread-pool" idle-thread-timeout-seconds="1800" />
        <thread-pool max-thread-pool-size="200" name="thread-pool-1" idle-thread-timeout-seconds="1800" />
      </thread-pools> 

List threads=new ArrayList();
螺纹工;

对于(int a=1;a更新:

我看到您正在使用Glassfish,并且说此中断每次都在15分钟内发生。Glassfish似乎被设置为在900秒左右超时,默认情况下为15分钟-这会引发中断异常

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

http.request-timeout-seconds
下面是更新属性的asadmin命令示例,但我尚未对其进行测试:

 # 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
我喜欢这个例子,因为它不只是留下一个空的catch子句,而是保留了在布尔值中调用它的事实。这样,当它最终完成时,您可以检查它是否被中断,如果是,请尊重它并中断当前线程

有关更多信息以及示例的来源,请参阅以下文章:
更改Glassfish中的domain.xml

      <thread-pools>
        <thread-pool name="http-thread-pool" idle-thread-timeout-seconds="1800" />
        <thread-pool max-thread-pool-size="200" name="thread-pool-1" idle-thread-timeout-seconds="1800" />
      </thread-pools> 


增加空闲线程超时秒数

很难理解这是一个问题的原因,除非您在某个地方发现它并错误地处理它。我在线程执行15分钟后遇到中断异常。我需要的是将时间限制增加到15分钟以上。@chamikaWKK为什么?当您发现它时,您在做什么?不确定您为什么要这样做o如果您实现了我在回答中提到的catch子句,那么就可以完全防止线程被中断,但是为了防止中断的发生,我已经用有关线程优先级的信息更新了我的回答。为什么高优先级会减少中断?它不能保证中断的减少,但调度程序可以将尝试为优先级较高的线程分配更多资源,如果其他线程的优先级较低,则可能会获得更多资源并“减少”中断,因为它将获得优先级。但是,这并不能保证,并且会使您的应用程序依赖于操作系统的任务调度程序的工作方式。这就是我建议上述方法的原因。
      <thread-pools>
        <thread-pool name="http-thread-pool" idle-thread-timeout-seconds="1800" />
        <thread-pool max-thread-pool-size="200" name="thread-pool-1" idle-thread-timeout-seconds="1800" />
      </thread-pools>