spring执行同步任务和异步任务

顾名思义:同步任务是指事情需要一件一件的做,做完当前的任务,才能开始做下一任务;异步任务是指做当前任务的同时,后台还可以在执行其他任务,可理解为可同时执行多任务,不必一件一件接着去做,下面开始上例子了

1.同步任务
Java代码spring执行同步任务和异步任务
文章图片

  1. /*
  2. * @(#)SyncTaskExecutorTest.java2011-4-27
  3. *
  4. * Copyright (c) 2011. All Rights Reserved.
  5. *
  6. */
  7. package org.jsoft.opensource.demos.spring.task;
  8. import org.junit.Test;
  9. import org.springframework.core.task.SyncTaskExecutor;
  10. /**
  11. * Spring同步任务处理
  12. *
  13. * @author Gerald Chen
  14. * @version $Id: SyncTaskExecutorTest.java,v 1.1 2011/05/30 08:58:07 gerald.chen Exp $
  15. */
  16. public class SyncTaskExecutorTest {
  17. @Test
  18. public void test() throws InterruptedException {
  19. SyncTaskExecutor executor = new SyncTaskExecutor();
  20. executor.execute(new OutThread());
  21. System.out.println("Hello, World!");
  22. Thread.sleep(10000 * 1000L);
  23. }
  24. static class OutThread implements Runnable {
  25. public void run() {
  26. for (int i = 0; i < 1000; i++) {
  27. System.out.println(i + " start ...");
  28. try {
  29. Thread.sleep(2 * 1000L);
  30. } catch (InterruptedException e) {
  31. // TODO Auto-generated catch block
  32. e.printStackTrace();
  33. }
  34. }
  35. }
  36. }
  37. }
spring执行同步任务和异步任务
文章图片

必须在线程任务执行完毕之后,"Hello,World!"才会被打印出来
2.异步任务
/*
Java代码spring执行同步任务和异步任务
文章图片
  1. * @(#)AsyncTaskExecutorTest.java2011-4-27
  2. *
  3. * Copyright (c) 2011. All Rights Reserved.
  4. *
  5. */
  6. package org.jsoft.opensource.demos.spring.task;
  7. import org.junit.Test;
  8. import org.springframework.core.task.AsyncTaskExecutor;
  9. import org.springframework.core.task.SimpleAsyncTaskExecutor;
  10. /**
  11. * Spring异步任务处理
  12. *
  13. * @author Gerald Chen
  14. * @version $Id: AsyncTaskExecutorTest.java,v 1.1 2011/05/30 08:58:07 gerald.chen Exp $
  15. */
  16. public class AsyncTaskExecutorTest {
  17. @Test
  18. public void test() throws InterruptedException {
  19. AsyncTaskExecutor executor = new SimpleAsyncTaskExecutor("sys.out");
  20. executor.execute(new OutThread(), 50000L);
  21. System.out.println("Hello, World!");
  22. Thread.sleep(10000 * 1000L);
  23. }
  24. static class OutThread implements Runnable {
  25. public void run() {
  26. for (int i = 0; i < 100; i++) {
  27. System.out.println(i + " start ...");
  28. try {
  29. Thread.sleep(2 * 1000L);
  30. } catch (InterruptedException e) {
  31. // TODO Auto-generated catch block
  32. e.printStackTrace();
  33. }
  34. }
  35. }
  36. }
  37. }
spring执行同步任务和异步任务
文章图片

"Hello,World!"被正常打印出来,线程任务在后台静静地执行.

3、AsyncTaskExecutor 调用带有返回值的多线程(实现callable接口),也是同步的

Java代码spring执行同步任务和异步任务
文章图片
  1. package org.jsoft.opensource.demos.spring.task;
  2. import java.util.concurrent.Callable;
  3. import java.util.concurrent.ExecutionException;
  4. import java.util.concurrent.Future;
  5. import org.junit.Test;
  6. import org.springframework.core.task.AsyncTaskExecutor;
  7. import org.springframework.core.task.SimpleAsyncTaskExecutor;
  8. /**
  9. * Spring异步任务处理 中易出错的
  10. *
  11. * @author blueram
  12. */
  13. public class AsyncTaskExecutorCallbleTest {
  14. @Test
  15. public void test() throws InterruptedException, ExecutionException {
  16. AsyncTaskExecutor executor = new SimpleAsyncTaskExecutor("sys.out");
  17. Future future = executor.submit(new OutThread());
  18. System.out.println(future.get());
  19. System.out.println("Hello, World!");
  20. Thread.sleep(10000 * 1000L);
  21. }
  22. static class OutThread implements Callable {
  23. public void run() {
  24. }
  25. @Override
  26. public String call() throws Exception {
  27. String ret = " i test callable";
  28. for (int i = 0; i < 10; i++) {
  29. try {
  30. Thread.sleep(2 * 1000L);
  31. System.out.println("i sleep 1s");
  32. } catch (InterruptedException e) {
  33. // TODO Auto-generated catch block
  34. e.printStackTrace();
  35. }
  36. }
  37. return ret;
  38. }
  39. }
  40. }

在使用中发现AsyncTaskExecutor 的方法submit也是阻塞型的,因此也可以认为是同步的。
spring执行同步任务和异步任务
文章图片


【spring执行同步任务和异步任务】 原文地址:http://hi.baidu.com/jadmin/item/eea662bc906676402aebe307

    推荐阅读