Home  >  Article  >  Java  >  Encapsulation of Java multi-threaded processing tasks

Encapsulation of Java multi-threaded processing tasks

伊谢尔伦
伊谢尔伦Original
2016-12-05 11:16:021279browse

Recently, multi-threading is used to process some tasks in many parts of the project. Logic code and Java multi-threading processing code are mixed together, resulting in extremely poor readability of the code. Now, the Java multi-threading related processing is extracted and reused in the aspect code. It’s not good, everyone is welcome to try it

The method of use is very simple, there are two ways to use it

1. Directly pass a batch of tasks to the multi-thread processing method and return the processing results

The code is as follows:

/**
 * Created with IntelliJ IDEA.
 * 测试多线程处理任务
 * className: TaskMulThreadServiceTest
 *
 * @version 1.0
 *          Date Time: a
 *@author: ddys
 */public class TaskMulThreadServiceTest extends TestCase implements ITaskHandle<String,Boolean>{

    public void testExecute() throws Exception {
        String [] taskItems = new String[100];        
            for (int i=0;i<100;i++){
            taskItems[i]="任务"+i;
        }
        IMulThreadService<String,Boolean> mulThreadService = new TaskMulThreadService(this);
        long start = System.currentTimeMillis();
        List<Boolean> result = mulThreadService.execute(taskItems);        
               for (Boolean e : result){            
                   if(!e){
                System.out.println("任务处理失败");
            }
        }
        System.out.println("所有任务处理完成,耗时"+(System.currentTimeMillis()-start)+",任务成功数"+result.size());
    }    
      /**
     * Created with IntelliJ IDEA.
     * 执行任务,返回所有执行的结果
     * className: TaskMulThreadService
     *
     * @author: ddys
     * @version 1.0
     * Date Time:
     */
    public Boolean execute(String s) {
        System.out.println(Thread.currentThread().getId()+"线程正在处理"+s);        
                   return true;
    }
}

2. Attached is a query task method, implement this query task method and business processing method, and then execute and return the processing results

The code is as follows:

ate Time: a
 *@author: XWK
 */
public class SelectTaskMulThreadServiceTest extends TestCase implements ISelectTask<String,Boolean>{
    public void testExecute() throws Exception {
        IMulThreadService<String,Boolean> mulThreadService = new SelectTaskMulThreadService(this);
        long start = System.currentTimeMillis();
        List<Boolean> result = mulThreadService.execute();        
        for (Boolean e : result){            
        if(!e){
                System.out.println("任务处理失败");
            }
        }
        System.out.println("所有任务处理完成,耗时"+(System.currentTimeMillis()-start)+",任务成功数"+result.size());
    }    
    /**
     * Created with IntelliJ IDEA.
     * 执行任务,返回所有执行的结果
     * className: TaskMulThreadService
     *
     * @author: ddys
     * @version 1.0
     * Date Time:
     */
    public Boolean execute(String s) {
        System.out.println(Thread.currentThread().getId()+"线程正在处理"+s);        
        return true;
    }    /**
     * @param &#39;a 传递参数
     * @return a 回类型
     * @throws
     * @Title: a
     * @Description: 获取一批任务
     * @author ddys
     * @date 2015-11-15 21:09
     */
    public String[] getTaskItem() {
        String [] taskItems = new String[100];        
        for (int i=0;i<100;i++){
            taskItems[i]="任务"+i;
        }        
        return taskItems;
    }
}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn