001package com.hammurapi.common.concurrent;
002
003import java.util.Collections;
004import java.util.List;
005import java.util.concurrent.AbstractExecutorService;
006import java.util.concurrent.ExecutorService;
007import java.util.concurrent.TimeUnit;
008
009/**
010 * Executes all tasks in the current thread. This executor service can be used in situations when
011 * inherently asynchronous class shall operate in synchronous mode. 
012 * @author Pavel Vlasov
013 *
014 */
015public class CallerThreadExecutorService extends AbstractExecutorService {
016        
017        private CallerThreadExecutorService() {
018                
019        }
020        
021        public final static ExecutorService INSTANCE = new CallerThreadExecutorService();
022
023        @Override
024        public void shutdown() {
025                // Nothing to do
026        }
027
028        @Override
029        public List<Runnable> shutdownNow() {
030                return Collections.emptyList();
031        }
032
033        @Override
034        public boolean isShutdown() {
035                return false;
036        }
037
038        @Override
039        public boolean isTerminated() {
040                return false;
041        }
042
043        @Override
044        public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
045                return true;
046        }
047
048        @Override
049        public void execute(Runnable command) {
050                command.run();
051        }
052
053}