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

时间:2014-12-15 15:28:51

标签: java java-ee

我有一个Web应用程序,其中包含一个用于执行可能长时间运行的作业的java bean。我想找到一种方法,当线程执行了很长时间后我可以识别,然后在必要时可能会将其删除。

我的应用程序在Glassfish 3中运行,因此我使用的是Java 1.6。我只是在寻找未来潜在问题的解决方案。

编辑:

要明确我正在寻找类似工具或实用程序的东西来监控正在运行的Web应用程序。

1 个答案:

答案 0 :(得分:0)

使用Executor服务。

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

然后你可以等待指定的时间:

  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();