Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.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_Timeout_Executorservice - Fatal编程技术网

Java 为什么不';我的线程失败时不会超时吗?

Java 为什么不';我的线程失败时不会超时吗?,java,multithreading,timeout,executorservice,Java,Multithreading,Timeout,Executorservice,不久前我问了一个问题:“如果线程花费的时间太长,我怎么能让线程被杀死?” 我已经实现了上面提到的解决方案,但在线程超时的某些罕见情况下,程序仍然可能失败/锁定(请参阅:保持main()方法打开,并防止程序的进一步cron运行) 以下是我使用的来源: //Iterate through the array to submit them into individual running threads. ExecutorService threadPool = Executors.newFi

不久前我问了一个问题:“如果线程花费的时间太长,我怎么能让线程被杀死?”

我已经实现了上面提到的解决方案,但在线程超时的某些罕见情况下,程序仍然可能失败/锁定(请参阅:保持main()方法打开,并防止程序的进一步cron运行)

以下是我使用的来源:

 //Iterate through the array to submit them into individual running threads.
    ExecutorService threadPool = Executors.newFixedThreadPool(12);
    List<Future<?>> taskList = new ArrayList<Future<?>>();
    for (int i = 0; i < objectArray.length; i++) {
        Future<?> task = threadPool.submit(new ThreadHandler(objectArray[i], i));
        taskList.add(task);
        Thread.sleep(500);
    }

    //Event handler to kill any threads that are running for more than 30 seconds (most threads should only need .25 - 1 second to complete.
    for(Future future : taskList){
        try{
            future.get(30, TimeUnit.SECONDS);
        }catch(CancellationException cx){ System.err.println("Cancellation Exception: "); cx.printStackTrace();
        }catch(ExecutionException ex){ System.err.println("Execution Exception: ");ex.printStackTrace();
        }catch(InterruptedException ix){ System.err.println("Interrupted Exception: ");ix.printStackTrace();
        }catch(TimeoutException ex) {future.cancel(true);}
    }
    threadPool.shutdown();
    threadPool.awaitTermination(60, TimeUnit.SECONDS);
//遍历数组,将它们提交到各个运行线程中。
ExecutorService线程池=Executors.newFixedThreadPool(12);
列表>();
for(int i=0;i

因此,我的问题是:实现了这段代码后,为什么executor服务在30秒时不停止工作。

因为我怀疑您的工作线程仍在运行。您正在调用
future.cancel(true)
但所做的只是在线程上设置中断标志——它不会主动中断正在运行的代码。另一种“中断”代码的方法是将一些
易失性布尔关闭
标志设置为true,并在代码中测试它。有关详细信息,请参阅此处

您需要确保
ThreadHandler
代码正确处理中断。例如,它需要检查
Thread.currentThread().isInterrupted()
在循环或其他代码块中。您还需要确保正确处理
中断异常
,而不仅仅是吞咽中断


有关线程中断的更多信息,请参阅。

每个任务的超时时间都在增加,这可能不是预期的。相反,您可以在超时后关闭线程池并取消其余任务

threadPool.shutdown();
threadPool.awaitTermination(30, TimeUnit.SECONDS);
threadPool.shutdownNow(); // interrupt any running tasks.

在《Java并发实践》一书中有一整章专门介绍了任务取消。根据我所读的内容,任务取消必须在finally块中,以确保任务始终结束

    try{
            future.get(30, TimeUnit.SECONDS);

        } catch (TimeoutException e) {
           // log error message and falls through to finally block
        } catch (ExecutionException e) {
            throw e;
        } finally {
            future.cancel(true); // interrupt task
        }
在处理InterruptedException时,必须恢复中断状态

           catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }

在ThreadHandler任务中,检查Thread.currentThread().isInterrupted()如果传播中断状态为true,则标记并抛出InterruptedException。

注意:每项任务的超时时间都会增加。例如,如果第一项任务需要29秒,则第二项任务需要59秒才能完成。您能否对如何修复此问题发表意见?因为这不是故意的。我已为超时时间较长的原因之一添加了答案/解决方案。H在我当前的数据结构中,这真的很难处理因此每个对象都有自己的线程。根本没有循环运行,如果我进行循环,我最终会多次使用每个对象,这会导致很多问题。在
对象的内部。run()
methods,您必须能够检测线程是否中断。这意味着您必须测试
thread.currentThread().isInterrupted()
在您的代码中。这些对象在做什么?好吧,每个线程都有自己的一组任务要做,但它们从在线源中提取特定的数据来预测事件……如果不破坏NDA,我不能说更多。不用担心。我的观点是,如果不检查代码中断或某些共享v,就没有简单的方法来停止代码变量。请参见此处:因此,我的基本问题是,我认为http请求超时存在问题。问题是,我只使用了一种“基本”方法获取它们:示例:URL urlObject=new URL(“);InputStream in=urlObject.openStream();从那里开始,我只使用outputstream并写入文件。假设我连接的网站通过syn/ack发送,但由于网络延迟或其他问题,甚至没有完成文件的发送,程序将挂起。我将无法“真正”插入线程。isinterrupted,因为它无法运行