Java 多執行緒任務的封裝

來源:互聯網
上載者:User

Java 多執行緒任務的封裝

 

最近公司項目很多地方使用多執行緒一些任務,邏輯代碼和java多執行緒代碼混合在一起,造成代碼的可讀性超級差,現在把Java多線程相關的處理抽出來,方面代碼中重複使用。抽的不好,歡迎大家拍磚

使用方法很簡單,有兩種使用方法

1.直接傳遞一批任務給到多執行緒方法,返回處理結果

代碼如下:

 
  1. /** 
  2.  * Created with IntelliJ IDEA. 
  3.  * 測試多執行緒任務 
  4.  * className: TaskMulThreadServiceTest 
  5.  * 
  6.  * @version 1.0 
  7.  *          Date Time: a 
  8.  *@author: ddys 
  9.  */ 
  10. public class TaskMulThreadServiceTest extends TestCase implements ITaskHandle<String,Boolean>{ 
  11.  
  12.     public void testExecute() throws Exception { 
  13.         String [] taskItems = new String[100]; 
  14.         for (int i=0;i<100;i++){ 
  15.             taskItems[i]="任務"+i; 
  16.         } 
  17.         IMulThreadService<String,Boolean> mulThreadService = new TaskMulThreadService(this); 
  18.         long start = System.currentTimeMillis(); 
  19.         List<Boolean> result = mulThreadService.execute(taskItems); 
  20.         for (Boolean e : result){ 
  21.             if(!e){ 
  22.                 System.out.println("任務處理失敗"); 
  23.             } 
  24.         } 
  25.         System.out.println("所有任務處理完成,耗時"+(System.currentTimeMillis()-start)+",任務成功數"+result.size()); 
  26.     } 
  27.  
  28.     /** 
  29.      * Created with IntelliJ IDEA. 
  30.      * 執行任務,返回所有執行的結果 
  31.      * className: TaskMulThreadService 
  32.      * 
  33.      * @author: ddys 
  34.      * @version 1.0 
  35.      * Date Time: 
  36.      */ 
  37.     public Boolean execute(String s) { 
  38.         System.out.println(Thread.currentThread().getId()+"線程正在處理"+s); 
  39.         return true; 
  40.     } 

2.附帶一個查詢任務的方法,實現這個查詢任務方法和業務處理方法,然後執行返回處理結果

代碼如下:

 
  1. ate Time: a 
  2.  *@author: XWK 
  3.  */ 
  4. public class SelectTaskMulThreadServiceTest extends TestCase implements ISelectTask<String,Boolean>{ 
  5.  
  6.     public void testExecute() throws Exception { 
  7.         IMulThreadService<String,Boolean> mulThreadService = new SelectTaskMulThreadService(this); 
  8.         long start = System.currentTimeMillis(); 
  9.         List<Boolean> result = mulThreadService.execute(); 
  10.         for (Boolean e : result){ 
  11.             if(!e){ 
  12.                 System.out.println("任務處理失敗"); 
  13.             } 
  14.         } 
  15.         System.out.println("所有任務處理完成,耗時"+(System.currentTimeMillis()-start)+",任務成功數"+result.size()); 
  16.     } 
  17.     /** 
  18.      * Created with IntelliJ IDEA. 
  19.      * 執行任務,返回所有執行的結果 
  20.      * className: TaskMulThreadService 
  21.      * 
  22.      * @author: ddys 
  23.      * @version 1.0 
  24.      * Date Time: 
  25.      */ 
  26.     public Boolean execute(String s) { 
  27.         System.out.println(Thread.currentThread().getId()+"線程正在處理"+s); 
  28.         return true; 
  29.     } 
  30.  
  31.     /** 
  32.      * @param 'a 傳遞參數 
  33.      * @return a 回類型 
  34.      * @throws 
  35.      * @Title: a 
  36.      * @Description: 擷取一批任務 
  37.      * @author ddys 
  38.      * @date 2015-11-15 21:09 
  39.      */ 
  40.     public String[] getTaskItem() { 
  41.         String [] taskItems = new String[100]; 
  42.         for (int i=0;i<100;i++){ 
  43.             taskItems[i]="任務"+i; 
  44.         } 
  45.         return taskItems; 
  46.     } 


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.