summary
stay jdk1.5 before , There are two ways to create a thread : One is direct inheritance Thread, The other is implementation Runnable Interface .
But this 2 There is a problem with both methods : Unable to get execution results after task execution . If you need to get the execution results , It must be achieved by sharing variables or message passing , It is more troublesome to use .
from Java
1.5 start , It provided Callable Class and Future class , Through them, we can get the result of task execution after the task is executed . After analysis and summary , We divide asynchronous computing threads into 3 part :
computational model
1, controller (Controller): Control thread of asynchronous computing , Responsible for the decomposition and initiation of asynchronous computing tasks , Leave the decomposed tasks to the asynchronous ones worker Thread to execute , After asynchronous computing is initiated ,controller Available Futrue Set of , Pass collection data to collector,collector according to Future To handle the results of asynchronous computation ;
2, processor (Wroker): Asynchronous computing thread , Responsible for specific calculation tasks ;
3, Result collector (Collector): Asynchronous calculation result collection thread , Get it from the initiating thread Future Set of , And responsible for monitoring Future Status of , according to Future To handle the results of asynchronous computation .
According to the above model , The asynchronous computation code is built as follows :
controller
package com.ips.concurrent.future; import java.util.ArrayList; import
java.util.List;import java.util.concurrent.ExecutorServic
Technology
Daily Recommendation