在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Java多线程处理任务的封装

2017-2-7 13:42| 发布者: zhangjf| 查看: 480| 评论: 0

摘要: 最近公司项目很多地方使用多线程处理一些任务,逻辑代码和多线程处理代码混合在一起,造成代码的可读性超级差,现在把多线程相关的处理抽出来,方面代码中重复使用。抽的不好,欢迎大家拍砖 使用方法很简单,有两种 ...

最近公司项目很多地方使用多线程处理一些任务,逻辑代码和多线程处理代码混合在一起,造成代码的可读性超级差,现在把多线程相关的处理抽出来,方面代码中重复使用。抽的不好,欢迎大家拍砖

使用方法很简单,有两种使用方法

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. public void testExecute() throws Exception {
  12. String [] taskItems = new String[100];
  13. for (int i=0;i<100;i++){
  14. taskItems[i]="任务"+i;
  15. }
  16. IMulThreadService<String,Boolean> mulThreadService = new TaskMulThreadService(this);
  17. long start = System.currentTimeMillis();
  18. List<Boolean> result = mulThreadService.execute(taskItems);
  19. for (Boolean e : result){
  20. if(!e){
  21. System.out.println("任务处理失败");
  22. }
  23. }
  24. System.out.println("所有任务处理完成,耗时"+(System.currentTimeMillis()-start)+",任务成功数"+result.size());
  25. }
  26. /**
  27. * Created with IntelliJ IDEA.
  28. * 执行任务,返回所有执行的结果
  29. * className: TaskMulThreadService
  30. *
  31. * @author: ddys
  32. * @version 1.0
  33. * Date Time:
  34. */
  35. public Boolean execute(String s) {
  36. System.out.println(Thread.currentThread().getId()+"线程正在处理"+s);
  37. return true;
  38. }
  39. }
复制代码

2.附带一个查询任务的方法,实现这个查询任务方法和业务处理方法,然后执行返回处理结果

代码如下:

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

项目主页:http://www.open-open.com/lib/view/home/1447752058822

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-9 10:17

Copyright 2015-2025 djqfx

返回顶部