I've a servlet tagged as @WebListener.

  public void contextInitialized(ServletContextEvent event) {                
        schedulerEngine = Executors.newSingleThreadScheduledExecutor();
        schedulerEngine.scheduleAtFixedRate(new Batch1(), 0, MILLISECONDS_ITERATE_PA, TimeUnit.MILLISECONDS);
        schedulerEngine.scheduleAtFixedRate(new Batch2(), 0, MILLISECONDS_ITERATE_IAS, TimeUnit.MILLISECONDS);
        schedulerEngine.scheduleAtFixedRate(new Batch3(), 0, MILLISECONDS_ITERATE_EV1, TimeUnit.MILLISECONDS);
        schedulerEngine.scheduleAtFixedRate(new Batch4(), 0, MILLISECONDS_ITERATE_MV1, TimeUnit.MILLISECONDS);
        schedulerEngine.scheduleAtFixedRate(new Batch5(), 0, MILLISECONDS_ITERATE_BB, TimeUnit.MILLISECONDS);
        schedulerEngine.scheduleAtFixedRate(new Batch6(), 0, MILLISECONDS_ITERATE_EV, TimeUnit.MILLISECONDS);
    }

public class BatchX implements Runnable {
        @Override
        public void run() {
         ....
        }
}

The MILLISECONDS_ITERATE_XX = 60000 (1 minute).

Is it possible that some thread stop itself for memory problems?

If in my BatchX() run method I have an exception (for example NullPointerException), the thread stop forever?

有帮助吗?

解决方案

It is always possible for a block of code to be interrupted by an exception. Whether this is fatal or not depends on what measures you take to prevent or recover from them:

  1. For checked exceptions e.g. IOException, you will need to put some recovery and/or logging code in your catch block and then decide if you wish to exit the method or continue executing.
  2. In the case of unchecked exceptions e.g. NullPointerException it's best to prevent them, not catch them, for example by making sure that no object can be null when you try to access it, or that no index may exceed the bounds of an array when iterating, etc.

Edit: To answer your particular question, the Javadoc says:

If any execution of the task encounters an exception, subsequent executions are suppressed. Otherwise, the task will only terminate via cancellation or termination of the executor.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top