001/*
002 * Copyright 2002-2012 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.core.task;
018
019import java.io.Serializable;
020
021import org.springframework.util.Assert;
022
023/**
024 * {@link TaskExecutor} implementation that executes each task <i>synchronously</i>
025 * in the calling thread.
026 *
027 * <p>Mainly intended for testing scenarios.
028 *
029 * <p>Execution in the calling thread does have the advantage of participating
030 * in it's thread context, for example the thread context class loader or the
031 * thread's current transaction association. That said, in many cases,
032 * asynchronous execution will be preferable: choose an asynchronous
033 * {@code TaskExecutor} instead for such scenarios.
034 *
035 * @author Juergen Hoeller
036 * @since 2.0
037 * @see SimpleAsyncTaskExecutor
038 */
039@SuppressWarnings("serial")
040public class SyncTaskExecutor implements TaskExecutor, Serializable {
041
042        /**
043         * Executes the given {@code task} synchronously, through direct
044         * invocation of it's {@link Runnable#run() run()} method.
045         * @throws IllegalArgumentException if the given {@code task} is {@code null}
046         */
047        @Override
048        public void execute(Runnable task) {
049                Assert.notNull(task, "Runnable must not be null");
050                task.run();
051        }
052
053}