import java.util.concurrent.Callable;
/**
-
@description: create a computing task class, implement the Callable interface, and rewrite the call method
-
@author: xz
-
@create: 2021-06-02 22:06
*/
public class ComputeTask implements Callable {
private String taskName;//Task name //Task constructor public ComputeTask(String taskName) { this.taskName = taskName; System.out.println("Start creating the calculation task. The calculation task name is:" + taskName); } //Method of calculating task @Override public Integer call() throws Exception { Integer result = 0; for (int i = 1; i <=50; i++) { result = +i; } System.out.println("[Calculation task]"+taskName +"Execution completed."); return result; }
}
**2,Create a test class**
package com.xz.thread.FutureTask;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
/**
-
@description:
-
@author: xz
-
@create: 2021-06-01 22:44
*/
public class Demo {
public static void main(String[] args) { //Task collection List<FutureTask<Integer>> futureTasks = new ArrayList<>(); //Create a fixed length thread pool ExecutorService pool = Executors.newFixedThreadPool(5); for (int i = 1; i <= 10; i++) { //Instantiate FutureTask and pass in the calculation task class FutureTask<Integer> futureTask = new FutureTask<>(new ComputeTask(i + "")); //Add to task collection futureTasks.add(futureTask); //Submit task to thread pool pool.submit(futureTask); } System.out.println("After all [calculation tasks] are submitted, the main thread starts to execute"); System.out.println("[Main thread task] start============"); //The main thread sleeps for 5 seconds and simulates the main thread to do some tasks try { Thread.sleep(5000); System.out.println("[Main thread task] start executing some tasks============"); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("[End of main thread task============"); //Used to print job execution results Integer result = 0; for (FutureTask<Integer> task : futureTasks) { try { //The get() method of FutureTask will automatically block until the task execution result is obtained result += task.get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } //Close thread pool pool.shutdown(); System.out.println("Multithreading multitasking execution results:" + result); }
}
**3,The output results are as follows:**
Start creating [calculation task]. Calculation task name: 1
Start to create [calculation task]. Calculation task name: 2
Start creating [calculation task]. Calculation task name: 3
Start to create [calculation task]. Calculation task name: 4
Start creating [calculation task]. Calculation task name: 5
Start creating [calculation task]. Calculation task name: 6
Start creating [calculation task]. Calculation task name: 7
Start creating [calculation task]. Calculation task name: 8
Start creating [calculation task]. Calculation task name: 9
Start creating [calculation task]. Calculation task name: 10
After all [calculation tasks] are submitted, the main thread starts to execute
[main thread task] start============
[calculation task] 1 execution completed.
[calculation task] 2 execution is completed.
[calculation task] 6 execution is completed.
[calculation task] 7 execution is completed.
[calculation task] 9 execution is completed.
[calculation task] 10 execution completed.
[calculation task] 8 execution completed.
last
That's all I want to share with you this time
The information is all in -*** My study notes: big factory interview real questions + microservices + MySQL+Java+Redis + algorithm + Network + Linux+Spring family bucket + JVM + study note map***
Finally, share a big gift package (learning notes) of the ultimate hand tearing architecture: distributed + microservices + open source framework + performance optimization
]9 execution completed.
[calculation task] 10 execution completed.
[calculation task] 8 execution completed.
last
That's all I want to share with you this time
The information is all in -*** My study notes: big factory interview real questions + microservices + MySQL+Java+Redis + algorithm + Network + Linux+Spring family bucket + JVM + study note map***
Finally, share a big gift package (learning notes) of the ultimate hand tearing architecture: distributed + microservices + open source framework + performance optimization