001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright 2005-2011 The ConQAT Project                                   |
004|                                                                          |
005| Licensed under the Apache License, Version 2.0 (the "License");          |
006| you may not use this file except in compliance with the License.         |
007| You may obtain a copy of the License at                                  |
008|                                                                          |
009|    http://www.apache.org/licenses/LICENSE-2.0                            |
010|                                                                          |
011| Unless required by applicable law or agreed to in writing, software      |
012| distributed under the License is distributed on an "AS IS" BASIS,        |
013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
014| See the License for the specific language governing permissions and      |
015| limitations under the License.                                           |
016+-------------------------------------------------------------------------*/
017package org.conqat.lib.commons.concurrent;
018
019import java.util.Collections;
020import java.util.List;
021import java.util.concurrent.AbstractExecutorService;
022import java.util.concurrent.TimeUnit;
023
024/**
025 * An executor service that executes everything within the caller's thread.
026 */
027public class InThreadExecutorService extends AbstractExecutorService {
028
029        /** Flags for shutdown. */
030        private boolean shutdown = false;
031
032        /** {@inheritDoc} */
033        @Override
034        public boolean awaitTermination(long timeout, TimeUnit unit) {
035                // always terminated (single thread)
036                return true;
037        }
038
039        /** {@inheritDoc} */
040        @Override
041        public boolean isShutdown() {
042                return shutdown;
043        }
044
045        /** {@inheritDoc} */
046        @Override
047        public boolean isTerminated() {
048                return true;
049        }
050
051        /** {@inheritDoc} */
052        @Override
053        public void shutdown() {
054                shutdown = true;
055        }
056
057        /** {@inheritDoc} */
058        @SuppressWarnings("unchecked")
059        @Override
060        public List<Runnable> shutdownNow() {
061                shutdown = true;
062                return Collections.EMPTY_LIST;
063        }
064
065        /** {@inheritDoc} */
066        @Override
067        public void execute(Runnable command) {
068                command.run();
069        }
070
071}