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:
- /**
- * Created with IntelliJ IDEA.
- * Testing multi-thread processing tasks
- * ClassName: TaskMulThreadServiceTest
- *
- * @ Version 1.0
- * Date Time:
- * @ 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] = "task" + I;
- }
- IMulThreadService <String, Boolean> mulThreadService = new TaskMulThreadService (this );
- Long start = System. currentTimeMillis ();
- List <Boolean> result = mulThreadService.exe cute (taskItems );
- For (Boolean e: result ){
- If (! E ){
- System. out. println ("task processing failed ");
- }
- }
- System. out. println ("all tasks are processed, time consumed" + (System. currentTimeMillis ()-start) + ", number of successful tasks" + result. size ());
- }
-
- /**
- * Created with IntelliJ IDEA.
- * Execute the task and return all execution results
- * ClassName: TaskMulThreadService
- *
- * @ Author: ddys
- * @ Version 1.0
- * Date Time:
- */
- Public Boolean execute (String s ){
- System. out. println (Thread. currentThread (). getId () + "the Thread is processing" + s );
- Return true;
- }
- }
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:
- Ate Time:
- * @ 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.exe cute ();
- For (Boolean e: result ){
- If (! E ){
- System. out. println ("task processing failed ");
- }
- }
- System. out. println ("all tasks are processed, time consumed" + (System. currentTimeMillis ()-start) + ", number of successful tasks" + result. size ());
- }
- /**
- * Created with IntelliJ IDEA.
- * Execute the task and return all execution results
- * ClassName: TaskMulThreadService
- *
- * @ Author: ddys
- * @ Version 1.0
- * Date Time:
- */
- Public Boolean execute (String s ){
- System. out. println (Thread. currentThread (). getId () + "the Thread is processing" + s );
- Return true;
- }
-
- /**
- * @ Param 'a pass the Parameter
- * @ Return a return type
- * @ Throws
- * @ Title:
- * @ Description: gets a batch of tasks.
- * @ Author ddys
- * @ Date
- */
- Public String [] getTaskItem (){
- String [] taskItems = new String [100];
- For (int I = 0; I <100; I ++ ){
- TaskItems [I] = "task" + I;
- }
- Return taskItems;
- }
- }