I believe everyone in the project encountered such a situation, the front desk needs to be quickly displayed, backstage also need to do a lot of logic. For example: The foreground click the Data Import button, the server after the button to perform logic A, and logical B (perform a large number of table data between the copy function), and then the front desk can not wait, to return to the front desk, to tell the process is in progress on the line. You need to use async here.
Click the button, logic a---logical-B (Async)---end of method.
In the end, the project needs are clear and the Executorservic thread pool is introduced.
Java provides four thread pools through executors, namely:
Newcachedthreadpool creates a cacheable thread pool that can flexibly reclaim idle threads if the thread pool length exceeds the processing needs, and creates a new thread if it is not recyclable.
Newfixedthreadpool creates a thread pool that controls the maximum number of concurrent threads, and the excess threads wait in the queue.
Newscheduledthreadpool creates a fixed-line pool that supports timed and recurring task execution.
Newsinglethreadexecutor creates a single threaded thread pool that performs tasks with only a single worker thread, ensuring that all tasks are executed in the specified order (FIFO, LIFO, priority).
Import Java.util.concurrent.callable;import Java.util.concurrent.executorservice;import java.util.concurrent.Executors;/** * @author szy * @version creation time: 2018-5-20 a.m. 10:25:06 **/ Public classTestasync {/** * @param args*/ Public Static voidMain (string[] args) {//TODO auto-generated Method Stub if(Task0 () = =true) {System. out. println ("Execute, see asynchronous result"); } } Public Static voidTask1 () {System. out. println ("Task1 is start"); } Public Static voidTask2 () {Executorservice executor= Executors.newfixedthreadpool (1); Executor.submit (Newcallable () {@Override PublicObject Call () throws Exception {//TODO auto-generated Method Stub//increased sleep time for easy viewing of results /*try {thread.sleep (10000); } catch (Interruptedexception e) {e.printstacktrace (); }*/ //Asynchronous Commit intsum =0; for(inti =0; I <10000; i++) {sum+=i; } System. out. println ("Task2 perform large import or export of data"); System. out. println ("task2="+sum); System. out. println ("task2 Import or export complete"); return NULL; } }); } Public Static voidTask3 () {System. out. println ("Task3 is start"); intj =0; while(true) { if(j + + >Ten) { Break; } System. out. println ("------------Task3 End-----------"); } } Public StaticBoolean task0 () {Task1 (); Task2 (); Task3 (); return true; }}
And then look at the results:
is is start------------task3 end-----------------------task3 End-----------------------Task3 End-----------------------Task3 End-----------------------TASK3 End-----------------------Task3 End-----------------------Task3 End-----------------------TASK3 End-----------------------Task3 End-----------------------Task3 end-----------------------Task3 End----------- task2 Execution of large import or export execution of data, see asynchronous result Task2=49995000task2 Import or export complete
As you can see, Task1 and Task3 are executed first, and the method ends directly without waiting for TASK2.
Asynchronous Task2 Another thread, executing itself. It has nothing to do with the main thread.
However, this is not visible in eclipse in the Deubug mode.
Java Executorservic thread pool (async)