zl程序教程

您现在的位置是:首页 >  Java

当前栏目

Java 多线程处理任务的封装

2023-03-09 21:57:42 时间

最近公司项目很多地方使用多线程处理一些任务,逻辑代码和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.     }