The thread pool in Java is very important, and it can save resource overhead and improve the performance of the program. Some Web servers, such as Tomcat, must use the thread pool. JAVA5 provides us with some API for applying the thread pool, and the following code will explain its usage.
Package Hxl.insist;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import Java.util.concurrent.scheduledexecutorservice;import Java.util.concurrent.timeunit;public Class Differentkindsthreadpool {public static void main (string[] args) {displaythreadpool ();d isplaycachedthreadpool (); Displaysinglethreadpool ();d isplayscheduledthreadpool ();} /** * Create a thread pool for a timed task */public static void Displayscheduledthreadpool () {Scheduledexecutorservice Scheduledthreadpool = Executors.newscheduledthreadpool (4);//It can perform tasks like a fixed thread pool Distributetaskforthreadpool (scheduledthreadpool);// This is its special, can be timed task Scheduledthreadpool.schedule (new Runnable () {@Overridepublic void run () {System.out.println ("Start performing task 1 ");}}, 5, timeunit.seconds);//re-scheduledthreadpool.scheduleatfixedrate the task again every 2 seconds (new Runnable () {@Overridepublic void Run () {System.out.println ("Start executing Task 2"),}}, 5, 3, timeunit.seconds);} /** * Creates a thread pool with only one threads, if the thread terminates, * He will create a new thread to join the pool, which will ensure that there is always a thread in the pool */public static void DisplaysinglethreAdpool () {Executorservice singlethreadpool = Executors.newsinglethreadexecutor ();d Istributetaskforthreadpool ( Singlethreadpool);} /** * Creates a thread pool that can create threads as needed, but * when the previously created thread is available, it reuses the previous line *, and if there are no available threads, a new thread * will be created and added to the pool. Threads that have not been used for 60 seconds will be terminated and removed from the cache */public static void Displaycachedthreadpool () {Executorservice Cachedthreadpool = Executors.newcachedthreadpool ();d istributetaskforthreadpool (Cachedthreadpool);} /** * Create a thread pool with fixed threads */public static void Displaythreadpool () {//Create a thread pool with 4 fixed threads executorservice ThreadPool = executors. Newfixedthreadpool (4);d istributetaskforthreadpool (ThreadPool);} /** * Assign 8 tasks to the thread pool so that it drives * @param threadPool */public static void Distributetaskforthreadpool (Executorservice threadPool) {/ /Let the thread pool drive 8 tasks for (int i = 1; I <= 8; i++) {//Because the inner class cannot put a non-final variable, I assign the value of I to taskfinal int task = I;threadpool.execute (n EW Runnable () {@Overridepublic void run () {System.out.println ("I Am" + thread.currentthread (). GetName () + "," + "got the first" + task + "A task, I started executing");}});}}
JAVA5 thread pool details and the use of the executors class to create different thread pools