| 1 | package com.hammurapi.common.concurrent; |
| 2 | |
| 3 | import java.util.Collections; |
| 4 | import java.util.List; |
| 5 | import java.util.concurrent.AbstractExecutorService; |
| 6 | import java.util.concurrent.ExecutorService; |
| 7 | import java.util.concurrent.TimeUnit; |
| 8 | |
| 9 | /** |
| 10 | * Executes all tasks in the current thread. This executor service can be used in situations when |
| 11 | * inherently asynchronous class shall operate in synchronous mode. |
| 12 | * @author Pavel Vlasov |
| 13 | * |
| 14 | */ |
| 15 | public class CallerThreadExecutorService extends AbstractExecutorService { |
| 16 | |
| 17 | private CallerThreadExecutorService() { |
| 18 | |
| 19 | } |
| 20 | |
| 21 | public final static ExecutorService INSTANCE = new CallerThreadExecutorService(); |
| 22 | |
| 23 | @Override |
| 24 | public void shutdown() { |
| 25 | // Nothing to do |
| 26 | } |
| 27 | |
| 28 | @Override |
| 29 | public List<Runnable> shutdownNow() { |
| 30 | return Collections.emptyList(); |
| 31 | } |
| 32 | |
| 33 | @Override |
| 34 | public boolean isShutdown() { |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | @Override |
| 39 | public boolean isTerminated() { |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public void execute(Runnable command) { |
| 50 | command.run(); |
| 51 | } |
| 52 | |
| 53 | } |