title: How to pause/restart SingleThreadExecutor (ExecutorService) in Java?
body: <p>I have implemented a Singleton class "SingleTaskExecutor" which is using SingleThreadExecutor to execute one task at a time in order. This class is used to perform asynchronous database operation one at time and in order. Everything is working fine. However now we have a use case wherein we need to pause this service for a while, execute some operation, and then resume the service. Not sure how to pause/resume the service. Here is my code:</p>
  
  <pre><code>public class SingleTaskExecutor {
  
      private SingleTaskExecutor() {}
      private final ExecutorService executor =   Executors.newSingleThreadExecutor();
  
      private static class LazyHolder {
          private static final SingleTaskExecutor instance = new SingleTaskExecutor(); 
      }
  
      public static SingleTaskExecutor getInstance() {
          return LazyHolder.instance;
      }
  
      public &lt;T&gt; Task&lt;T&gt; create(Callable&lt;T&gt; callable) {
          Task&lt;T&gt; task = new Task&lt;T&gt;() {
              @Override
              protected T call() throws Exception {
                  return callable.call();
              }
          };
          return task;
      }
  
      public &lt;T&gt; T execute(Task&lt;T&gt; task) {
          return (T) executor.submit(task);
      }
  
      public void shutDown() {
          executor.shutdown();
      }
  
      public void awaitTermination() {
          try {
              executor.awaitTermination(5, TimeUnit.SECONDS);
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
      }
  
      public void waitPlease() {
          try {
              Thread.currentThread().wait();
              //executor.wait();
          } catch (InterruptedException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  
      public void resume() {
          Thread.currentThread().notifyAll();
          //executor.notifyAll();
      }
  }
  </code></pre>