1 /* 2 * Copyright 2002-2006 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.springframework.core.task; 18 19 import org.springframework.util.Assert; 20 21 import java.io.Serializable; 22 23 /** 24 * <code>TaskExecutor</code> implementation that executes each task 25 * <i>synchronously</i> in the calling thread. 26 * 27 * <p>Mainly intended for testing scenarios. 28 * 29 * <p>Execution in the calling thread does have the advantage of participating 30 * in it's thread context, for example the thread context class loader or the 31 * thread's current transaction association. That said, in many cases, 32 * asynchronous execution will be preferable: choose an asynchronous 33 * <code>TaskExecutor</code> instead for such scenarios. 34 * 35 * @author Juergen Hoeller 36 * @see SimpleAsyncTaskExecutor 37 * @see org.springframework.scheduling.timer.TimerTaskExecutor 38 * @since 2.0 39 */ 40 public class SyncTaskExecutor implements TaskExecutor, Serializable { 41 42 /** 43 * Executes the given <code>task</code> synchronously, through direct 44 * invocation of it's {@link Runnable#run() run()} method. 45 * @throws IllegalArgumentException if the given <code>task</code> is <code>null</code> 46 */ 47 public void execute(Runnable task) { 48 Assert.notNull(task, "Runnable must not be null"); 49 task.run(); 50 } 51 52 } 53