简体   繁体   中英

Java Sleep-interupted exception

Could someone help explain this error:

    private static ExecutorService es = Executors.newFixedThreadPool(1);;
    private static List<Future<?>> futures = new ArrayList<Future<?>>();

    futures.add(es.submit(new Callable<Object>() {
        public Object call() throws Exception {
          try{
               Polling.start();      //has an while(true) loop
          } catch (Exception e){
              e.printStackTrace();
          }
            return null; 
        }
    }));

    Thread.sleep(2000);

    System.out.println("stopping...");
    es.shutdownNow();
    futures.clear();

This gives:

java.lang.InterruptedException: sleep interrupted

How to avoid above exception, by processing delay in main method?

Stack trace

java.lang.InterruptedException: sleep interrupted
    at java.lang.Thread.sleep(Native Method)
    at bus.Polling.start(Polling.java:107)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:680)

The InterruptedException is the Java mechanism used to let an Executor tell its Callables to stop what they are doing, clean up and exit.

If you do not want to see it, then catch it and end your Callable properly.

You're just doing what you want to achieve. As your Polling-Class has an infinite loop, the interrupt is a suitable way to stop it. As for the Exception: catch it inside the Polling-Class and exit the for-loop when it occurs.

You are setting up a callable which keeps sleeping in an endless loop, then later forcibly terminate that callable by shutting down the whole executor service. If your callable terminated normally in time, or you didn't forcibly shut it down, you wouldn't get this exception.

What are you actually trying to achieve with this code?

The thread executing your callable is being interrupted because the main thread finishes and exits. There are several ways to address this issue. One way is to declare the executing thread as a daemon:

ExecutorService pool = Executors.newSingleThreadExecutor(new ThreadFactory() {
   @Override
   public Thread newThread(Runnable runnable) {
      Thread thread = Executors.defaultThreadFactory().newThread(runnable);
      thread.setDaemon(true);
      return thread;
   }
});

but it really depends on what you're trying to achieve.

Use of es.shutdown() in preference over es.shutDownNow() should make the InterruptedException go away. However, unless the context is clearer it is difficult to answer the question.

Responding to your exaplaining the context :

1) Submit a task to the executor service and obtain the corresponding future.

2) Using this future whenever the appropriate event is received to cancel the task. Invoke future.cancel(true).

3) In your future task, observe if the thread has been interrupted in which case you break out from the loop and let the task complete.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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