The Java.util.concurrent.ExecutorService interface provides a number of methods for threading management
Method |
Description |
Shutdown |
Refuses to receive new tasks, closes when the submitted task is executed, and does not block the host thread, if blocking is required to implement with Awaittermination |
Shutdownnow |
Stops all tasks that are in progress, suspends tasks that are not executed, and shuts down, and the host thread does not block, and can be implemented with awaittermination if blocking is required |
Awaittermination |
When shutdown occurs, blocks the host thread until the agreed time is over or all tasks are completed |
Submit |
Submit task Callable/runnable, can use the Get () method of the future to block the host thread until the end of the task to return results |
With these methods, you can implement various functions of the thread pool based on this interface (for example, java.util.concurrent.threadpoolexecutor/ Java.util.concurrent.ScheduledThreadPoolExecutor), take Java.util.concurrent.ThreadPoolExecutor as an example, with a detailed explanation of its parameters
Name |
Type |
description |
Corepo olsize |
int |
thread pool minimum number of threads |
maximumpoolsize |
int |
thread pool most Large number of threads |
keepalivetime |
long |
thread idle time, if the number of threads is greater than corepoolsize, threads with idle time exceeding this value will be stopped for recycling |
Unit |
timeunit |
keepalivetime time Unit |
workqueue |
Blockingqueue<runnable> |
committed but not executed task queue |
threadfactory |
Threa Dfactory |
Create a new thread factory |
handler |
rejectedexecutionhandler |
when the thread pool or queue reaches Handling class |
When a new task throws an exception to the upper limit
At the same time, the Java.util.concurrent.Executors class provides tool methods based on the Java.util.concurrent.ThreadPoolExecutor class, and the common methods are
Method |
Description |
Newfixedthreadpool |
Thread pool with a fixed number of threads |
Newsinglethreadexecutor |
The thread pool contains only one worker thread |
Newcachedthreadpool |
Create threads on Demand, if no threads are available in the thread pool, create a new thread and join until the number of threads reaches the upper limit (integer.max_value) |
The test code is as follows
Importjava.util.Date;Importjava.util.concurrent.Callable;Importjava.util.concurrent.ExecutionException;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;Importjava.util.concurrent.Future;ImportJava.util.concurrent.TimeUnit;ImportOrg.junit.Assert;Importorg.junit.Test;/*** @Description: Test Executorservice*/ Public classThreadexecutorservicetest {Private Static FinalString this_is_shutdown_with_await_termination = "This is Shutdownwithawaittermination"; Private Static Final intRESULT = 111; Private Static BooleanSubmitrunnable ()throwsinterruptedexception, executionexception {executorservice executorservice=Executors.newsinglethreadexecutor (); Future<?> future = Executorservice.submit (NewRunnable () {@Override Public voidrun () {System.out.println ("This is submitrunnable"); } }); returnFuture.get () = =NULL; } Private StaticInteger Submitrunnablewithresult ()throwsinterruptedexception, executionexception {executorservice executorservice=Executors.newsinglethreadexecutor (); Future<Integer> future = Executorservice.submit (NewRunnable () {@Override Public voidrun () {System.out.println ("This is Submitrunnablewithresult"); }}, RESULT); returnFuture.get (); } Private StaticInteger submitblockcallable ()throwsinterruptedexception, executionexception {executorservice executorservice= Executors.newfixedthreadpool (1); Future<Integer> future = Executorservice.submit (NewCallable<integer>() {@Override PublicInteger Call ()throwsException {System.out.println ("This is submitblockcallable"); returnRESULT; } }); returnFuture.get ();//Blocking } Private Static BooleanSubmitnonblockcallable ()throwsinterruptedexception, executionexception {executorservice executorservice= Executors.newfixedthreadpool (1); Future<Integer> future = Executorservice.submit (NewCallable<integer>() {@Override PublicInteger Call ()throwsException {System.out.println ("This is submitnonblockcallable"); returnRESULT; } }); while(!future.isdone ()) {//non-blockingSystem.out.println (NewDate ()); } returnFuture.isdone (); } Private StaticString shutdown ()throwsinterruptedexception, executionexception {executorservice executorservice= Executors.newfixedthreadpool (1); FinalStringBuilder SB =NewStringBuilder (); Executorservice.submit (NewCallable<integer>() {@Override PublicInteger Call ()throwsException {thread.sleep (10000); Sb.append ("This is shutdown"); returnRESULT; } }); Executorservice.shutdown (); returnsb.tostring (); } Private StaticString shutdownwithawaittermination ()throwsinterruptedexception, executionexception {executorservice executorservice= Executors.newfixedthreadpool (1); FinalStringBuilder SB =NewStringBuilder (); Executorservice.submit (NewCallable<integer>() {@Override PublicInteger Call ()throwsException {thread.sleep (10000); Sb.append (this_is_shutdown_with_await_termination); returnRESULT; } }); Executorservice.shutdown (); Executorservice.awaittermination (Integer.max_value, timeunit.days); returnsb.tostring (); } @Test Public voidTest ()throwsinterruptedexception, executionexception {assert.asserttrue (submitrunnable ()); Assert.assertequals (RESULT, Submitrunnablewithresult (). Intvalue ()); Assert.assertequals (RESULT, Submitblockcallable (). Intvalue ()); Assert.asserttrue (Submitnonblockcallable ()); Assert.asserttrue (Shutdown (). IsEmpty ()); Assert.assertequals (This_is_shutdown_with_await_termination, Shutdownwithawaittermination ()); }}
Java Multithreaded series seven--executorservice