顾名思义:同步任务是指事情需要一件一件的做,做完当前的任务,才能开始做下一任务;异步任务是指做当前任务的同时,后台还可以在执行其他任务,可理解为可同时执行多任务,不必一件一件接着去做,下面开始上例子了
1.同步任务
Java代码
文章图片
- /*
- * @(#)SyncTaskExecutorTest.java2011-4-27
- *
- * Copyright (c) 2011. All Rights Reserved.
- *
- */
- package org.jsoft.opensource.demos.spring.task;
- import org.junit.Test;
- import org.springframework.core.task.SyncTaskExecutor;
- /**
- * Spring同步任务处理
- *
- * @author Gerald Chen
- * @version $Id: SyncTaskExecutorTest.java,v 1.1 2011/05/30 08:58:07 gerald.chen Exp $
- */
- public class SyncTaskExecutorTest {
- @Test
- public void test() throws InterruptedException {
- SyncTaskExecutor executor = new SyncTaskExecutor();
- executor.execute(new OutThread());
- System.out.println("Hello, World!");
- Thread.sleep(10000 * 1000L);
- }
- static class OutThread implements Runnable {
- public void run() {
- for (int i = 0; i < 1000; i++) {
- System.out.println(i + " start ...");
- try {
- Thread.sleep(2 * 1000L);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- }
![spring执行同步任务和异步任务](https://img.it610.com/image/info8/c98747cc64e14913897c98bbc4f2eb9f.jpg)
文章图片
必须在线程任务执行完毕之后,"Hello,World!"才会被打印出来
2.异步任务
/*
Java代码
![spring执行同步任务和异步任务](https://img.it610.com/image/info8/f3075d0a3db44b1bb82add28d75d45ad.jpg)
文章图片
- * @(#)AsyncTaskExecutorTest.java2011-4-27
- *
- * Copyright (c) 2011. All Rights Reserved.
- *
- */
- package org.jsoft.opensource.demos.spring.task;
- import org.junit.Test;
- import org.springframework.core.task.AsyncTaskExecutor;
- import org.springframework.core.task.SimpleAsyncTaskExecutor;
- /**
- * Spring异步任务处理
- *
- * @author Gerald Chen
- * @version $Id: AsyncTaskExecutorTest.java,v 1.1 2011/05/30 08:58:07 gerald.chen Exp $
- */
- public class AsyncTaskExecutorTest {
- @Test
- public void test() throws InterruptedException {
- AsyncTaskExecutor executor = new SimpleAsyncTaskExecutor("sys.out");
- executor.execute(new OutThread(), 50000L);
- System.out.println("Hello, World!");
- Thread.sleep(10000 * 1000L);
- }
- static class OutThread implements Runnable {
- public void run() {
- for (int i = 0; i < 100; i++) {
- System.out.println(i + " start ...");
- try {
- Thread.sleep(2 * 1000L);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- }
![spring执行同步任务和异步任务](https://img.it610.com/image/info8/fe8ca84e01844ab7ace698bbe0ec5e4d.jpg)
文章图片
"Hello,World!"被正常打印出来,线程任务在后台静静地执行.
3、AsyncTaskExecutor 调用带有返回值的多线程(实现callable接口),也是同步的
Java代码
![spring执行同步任务和异步任务](https://img.it610.com/image/info8/f3075d0a3db44b1bb82add28d75d45ad.jpg)
文章图片
- package org.jsoft.opensource.demos.spring.task;
- import java.util.concurrent.Callable;
- import java.util.concurrent.ExecutionException;
- import java.util.concurrent.Future;
- import org.junit.Test;
- import org.springframework.core.task.AsyncTaskExecutor;
- import org.springframework.core.task.SimpleAsyncTaskExecutor;
- /**
- * Spring异步任务处理 中易出错的
- *
- * @author blueram
- */
- public class AsyncTaskExecutorCallbleTest {
- @Test
- public void test() throws InterruptedException, ExecutionException {
- AsyncTaskExecutor executor = new SimpleAsyncTaskExecutor("sys.out");
- Future future = executor.submit(new OutThread());
- System.out.println(future.get());
- System.out.println("Hello, World!");
- Thread.sleep(10000 * 1000L);
- }
- static class OutThread implements Callable {
- public void run() {
- }
- @Override
- public String call() throws Exception {
- String ret = " i test callable";
- for (int i = 0; i < 10; i++) {
- try {
- Thread.sleep(2 * 1000L);
- System.out.println("i sleep 1s");
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- return ret;
- }
- }
- }
在使用中发现AsyncTaskExecutor 的方法submit也是阻塞型的,因此也可以认为是同步的。
![spring执行同步任务和异步任务](https://img.it610.com/image/info8/bd30535ec2284035bc8bf98627b7b03b.png)
文章图片
【spring执行同步任务和异步任务】 原文地址:http://hi.baidu.com/jadmin/item/eea662bc906676402aebe307