Encapsulation of Java multi-thread processing tasks

Source: Internet
Author: User

Encapsulation of Java multi-thread processing tasks

 

Recently, many projects in the company use multiple threads to process some tasks. The logic code and java multi-thread processing code are mixed together, resulting in extremely poor code readability. Now we can extract Java multi-thread-related processing, this is used repeatedly in the aspect code. You are welcome to make a picture.

It is easy to use. 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:

 
 
  1. /**
  2. * Created with IntelliJ IDEA.
  3. * Testing multi-thread processing tasks
  4. * ClassName: TaskMulThreadServiceTest
  5. *
  6. * @ Version 1.0
  7. * Date Time:
  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] = "task" + I;
  16. }
  17. IMulThreadService <String, Boolean> mulThreadService = new TaskMulThreadService (this );
  18. Long start = System. currentTimeMillis ();
  19. List <Boolean> result = mulThreadService.exe cute (taskItems );
  20. For (Boolean e: result ){
  21. If (! E ){
  22. System. out. println ("task processing failed ");
  23. }
  24. }
  25. System. out. println ("all tasks are processed, time consumed" + (System. currentTimeMillis ()-start) + ", number of successful tasks" + result. size ());
  26. }
  27.  
  28. /**
  29. * Created with IntelliJ IDEA.
  30. * Execute the task and return all execution results
  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 () + "the Thread is processing" + s );
  39. Return true;
  40. }
  41. }

2. A Query Task method is included to implement the Query Task method and business processing method, and then execute and return the processing result.

The Code is as follows:

 
 
  1. Ate Time:
  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.exe cute ();
  10. For (Boolean e: result ){
  11. If (! E ){
  12. System. out. println ("task processing failed ");
  13. }
  14. }
  15. System. out. println ("all tasks are processed, time consumed" + (System. currentTimeMillis ()-start) + ", number of successful tasks" + result. size ());
  16. }
  17. /**
  18. * Created with IntelliJ IDEA.
  19. * Execute the task and return all execution results
  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 () + "the Thread is processing" + s );
  28. Return true;
  29. }
  30.  
  31. /**
  32. * @ Param 'a pass the Parameter
  33. * @ Return a return type
  34. * @ Throws
  35. * @ Title:
  36. * @ Description: gets a batch of tasks.
  37. * @ Author ddys
  38. * @ Date
  39. */
  40. Public String [] getTaskItem (){
  41. String [] taskItems = new String [100];
  42. For (int I = 0; I <100; I ++ ){
  43. TaskItems [I] = "task" + I;
  44. }
  45. Return taskItems;
  46. }
  47. }


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.