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

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

 

1.同步任务

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

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

必须在线程任务执行完毕之后,"Hello,World!"才会被打印出来

2.异步任务

 /*

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

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

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

 

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

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

 

在使用中发现AsyncTaskExecutor 的方法submit也是阻塞型的,因此也可以认为是同步的。

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

 

原文地址:http://hi.baidu.com/jadmin/item/eea662bc906676402aebe307