java在超时值后终止线程

java在超时值后终止线程,java,Java,我在java中有一个要求,我希望线程在特定的时间段(比如开始处理的1分钟)后死亡并自杀。java是否为此提供了一种方法 这里要添加的一点是,我正在使用ThreadPoolExecutor并将可运行对象提交给ThreadPoolExecutor,以便在队列中执行。这是我们框架的一部分,我无法删除ThreadPoolExecutor。鉴于此,我如何使用ExecutorService?问题与此相同。请使用ExecutorService来执行Runnable这与下面的问题相同。请使用ExecutorSe

我在java中有一个要求,我希望线程在特定的时间段(比如开始处理的1分钟)后死亡并自杀。java是否为此提供了一种方法


这里要添加的一点是,我正在使用ThreadPoolExecutor并将可运行对象提交给ThreadPoolExecutor,以便在队列中执行。这是我们框架的一部分,我无法删除ThreadPoolExecutor。鉴于此,我如何使用ExecutorService?

问题与此相同。请使用ExecutorService来执行Runnable

这与下面的问题相同。请使用ExecutorService执行Runnable

将ExecutorService与Future.get或invoke*样式的方法一起使用,这将为您提供所需的功能。但是,您可以随时简单地定期检查线程内是否已达到超时,以便线程可以正常退出。

将ExecutorService与Future.get或invoke*样式的方法一起使用,这将为您提供所需的功能。但是,您可以随时简单地定期检查线程内是否已达到超时,以便线程可以正常退出。

您可以中断线程,但根据您在工作线程中的操作,您需要手动检查thread.isInterrupted。有关详细信息,请参阅。

您可以中断线程,但根据您在工作线程中执行的操作,您需要手动检查thread.isInterrupted。有关详细信息,请参阅。

您可以尝试Thread.interrupt,并在线程本身中使用Thread.isInterrupted检查中断。或者,如果线程休眠了一段时间,那么只需在InterruptedException上退出即可。但这对你所做的事情很重要。如果您正在等待IO任务,请尝试关闭流并退出线程中抛出的IOException。

您可以尝试Thread.interrupt,并在线程本身中使用Thread.isInterrupted检查中断。或者,如果线程休眠了一段时间,那么只需在InterruptedException上退出即可。但这对你所做的事情很重要。如果您正在等待IO任务,请尝试关闭流并退出线程中抛出的IOException。

您不能只杀死线程,因为线程可以持有锁或其他资源(如文件)。相反,将stop方法添加到您在线程中执行的Runnable中,这将设置内部标志并定期在run方法中检查它。像这样:

class StopByPollingThread implements Runnable {
    private volatile boolean stopRequested;
    private volatile Thread thisThread;

    synchronized void start() {
        thisThread = new Thread(this);
        thisThread.start();
    }

    @Override
    public void run() {
        while (!stopRequested) {
            // do some stuff here
            // if stuff can block interruptibly (like calling Object.wait())
            // you'll need interrupt thread in stop() method as well
            // if stuff can block uninterruptibly (like reading from socket)
            // you'll need to close underlying socket to awake thread
            try {
                wait(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    synchronized void requestStop() {
        stopRequested = true;
        if (thisThread != null)
            thisThread.interrupt();
    }
}

此外,您可能希望阅读Goetz的Java并发性实践。

您不能仅仅杀死线程,因为线程可以持有锁或其他资源,如文件。相反,将stop方法添加到您在线程中执行的Runnable中,这将设置内部标志并定期在run方法中检查它。像这样:

class StopByPollingThread implements Runnable {
    private volatile boolean stopRequested;
    private volatile Thread thisThread;

    synchronized void start() {
        thisThread = new Thread(this);
        thisThread.start();
    }

    @Override
    public void run() {
        while (!stopRequested) {
            // do some stuff here
            // if stuff can block interruptibly (like calling Object.wait())
            // you'll need interrupt thread in stop() method as well
            // if stuff can block uninterruptibly (like reading from socket)
            // you'll need to close underlying socket to awake thread
            try {
                wait(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    synchronized void requestStop() {
        stopRequested = true;
        if (thisThread != null)
            thisThread.interrupt();
    }
}

此外,您可能想阅读Goetz的《Java并发性实践》。

您想用这个线程实现什么?IO任务?不是IO任务,此线程调用第三方Web服务,并且挂起时间超过15分钟。因此,我想消除这个线程,即IO。任何像调用Web服务这样连接到服务器的流都是IO。在这种情况下,只需关闭它正在使用的插座。这不是最优雅的解决方案,但它应该可以工作。你想用这个线程实现什么?IO任务?不是IO任务,此线程调用第三方Web服务,并且挂起时间超过15分钟。因此,我想消除这个线程,即IO。任何像调用Web服务这样连接到服务器的流都是IO。在这种情况下,只需关闭它正在使用的插座。这不是最优雅的解决方案,但它应该可以工作。当线程创建和销毁得太快时,ExecutorService往往表现得不太好。似乎它经常被困在关闭线程上。您知道还有其他替代方案吗?当线程创建和销毁得太快时,ExecutorService往往表现得不太好。似乎它经常被困在关闭线程上。你知道还有其他选择吗?