View Javadoc

1   package com.atlassian.sal.core.executor;
2   
3   import java.util.List;
4   import java.util.concurrent.AbstractExecutorService;
5   import java.util.concurrent.ExecutorService;
6   import java.util.concurrent.TimeUnit;
7   
8   /**
9    * Executor service that wraps executing callables and runnables in a wrapper that transfers the thread local state of
10   * the caller to the thread of the executing task.
11   *
12   * @since 2.0
13   */
14  public class ThreadLocalDelegateExecutorService extends AbstractExecutorService
15  {
16      protected final ThreadLocalContextManager manager;
17      private final ExecutorService delegate;
18  
19      public ThreadLocalDelegateExecutorService(ThreadLocalContextManager manager, ExecutorService delegate)
20      {
21          this.manager = manager;
22          this.delegate = delegate;
23      }
24  
25      public void shutdown()
26      {
27          delegate.shutdown();
28      }
29  
30      public List<Runnable> shutdownNow()
31      {
32          return delegate.shutdownNow();
33      }
34  
35      public boolean isShutdown()
36      {
37          return delegate.isShutdown();
38      }
39  
40      public boolean isTerminated()
41      {
42          return delegate.isTerminated();
43      }
44  
45      public boolean awaitTermination(long timeout, TimeUnit unit)
46          throws InterruptedException
47      {
48          return delegate.awaitTermination(timeout, unit);
49      }
50  
51      public void execute(Runnable command)
52      {
53          delegate.execute(new ThreadLocalDelegateRunnable(manager, command));
54      }
55  }