简体   繁体   English

如何在java Web容器中监视/终止长时间运行的线程?

[英]How do you monitor/terminate long-running threads in a java web container?

I have a web application that contains a java bean for executing a potentially long-running job. 我有一个Web应用程序,其中包含一个用于执行可能长时间运行的作业的java bean。 I'd like to find a way that I can identify when a thread has been executing for a very long time and then potentially kill it if necessary. 我想找到一种方法,我可以识别线程已经执行了很长时间,然后在必要时可能会杀死它。

My application runs in Glassfish 3 so I am on Java 1.6. 我的应用程序在Glassfish 3中运行,因此我使用的是Java 1.6。 I am just looking for a solution to a potential problem in the future. 我只是在寻找未来潜在问题的解决方案。

EDIT: 编辑:

To be clear I am looking for something like a tool or utility to monitor a running web application. 要清楚我正在寻找像监视正在运行的Web应用程序的工具或实用程序。

Use an Executor Service. 使用Executor服务。

  ExecutorService executor = Executors.newSingleThreadExecutor();
  Future<String> future = executor.submit(new Runnable(){ ....});//pass your runnable

And then you can wait for a specified time: 然后你可以等待指定的时间:

  try {
        int timeOut = 5;
        //Waits if necessary for at most the given time for the computation to
        // complete, and then retrieves its result, if available.
         future.get(timeout, TimeUnit.SECONDS)
    } catch (TimeoutException e) {
        System.out.println("TimedOut!");
    }

    executor.shutdownNow();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM