Using the thread pool is much more efficient than a single thread at the same order of magnitude. The thread pool can reduce the overhead associated with creating threads. Instead of actually destroying the thread, the threads in the thread pool end up with a recycle operation. In this process, the memory consumption of the thread pool will certainly be greater than a single thread. Be cautious about this when using the thread pool. Here are two ways to test each one.
Import Java.util.linkedlist;import Java.util.list;import Java.util.random;import Java.util.concurrent.linkedblockingqueue;import Java.util.concurrent.threadpoolexecutor;import Java.util.concurrent.timeunit;public class ThreadpoolDemo1 {static void Usethreadpool (int count) {//define Storage collection final List <Integer> list = new linkedlist<integer> (); Long startTime = System.currenttimemillis (); Threadpoolexecutor TPE = new Threadpoolexecutor (1, 1, timeunit.seconds, new linkedblockingqueue<runnable> ( count));//Generate random number final random random = new random (); for (int i=0; i < count; i++) {Tpe.execute (new Runnable () {@Override public void Run () {List.add (Random.nextint ());}});} Tpe.shutdown (); try {tpe.awaittermination (1, timeunit.days);} catch (Interruptedexception e) {//TODO auto-generated Catch Blocke.printstacktrace ();} System.out.println (System.currenttimemillis ()-startTime); System.out.println (List.size ());} static void Useonethread (int count) {final list<integer> List = new LinkedlIst<integer> (); Long startTime = System.currenttimemillis ();//Generate random number final random random = new random (); for (int i=0; i<count; i++) {Thread thread = new Thread () {public void run () {List.add (Random.nextint ());}}; Thread.Start (); try {thread.join ();} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} System.out.println (System.currenttimemillis ()-startTime); System.out.println (List.size ());} public static void Main (string[] args) {usethreadpool (20000); Useonethread (20000);}}
When the count parameter is the same value, using the thread pool is several times faster than creating the threads individually.
Benefits of using the thread pool:
1) reduces the number of times the thread is created and destroyed, and the thread can be reused
2) threads in the thread pool can be adjusted by the programmer. Reduce the consumption of system memory.
Use of the thread pool:
Import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import Java.util.concurrent.scheduledthreadpoolexecutor;import Java.util.concurrent.timeunit;public Class ThreadPoolDemo2 {//Create a reusable single-line castle private void Testsinglethreadexecutor () {Executorservice pool = Executors.newsinglethreadexecutor (); Thread T1 = new MyThread (); Thread t2 = new MyThread (); Thread t3 = new MyThread ();p ool.execute (T1);p ool.execute (T2);p Ool.execute (T3);p ool.shutdown ();} Create a fixed-size thread pool private void Testfixedthreadpool () {Executorservice pool = Executors.newfixedthreadpool (3); Thread T1 = new MyThread (); Thread t2 = new MyThread (); Thread t3 = new MyThread ();p ool.execute (T1);p ool.execute (T2);p Ool.execute (T3);p ool.shutdown ();} Create a thread pool with timer function private void Testscheduledthreadpoolexecutor () {Scheduledthreadpoolexecutor poolexecutor = new Scheduledthreadpoolexecutor (1);p oolexecutor.scheduleatfixedrate (new Runnable () {//Every once in a while to trigger an exception @overridepublic void Run () {//throw new RuntimeException (); System.outThe. println ("================"),}, Timeunit.milliseconds, and the;p oolexecutor.scheduleatfixedrate (new Runnable ( {//Print the system time at intervals to prove that both are non-affected @overridepublic void run () {System.out.println (System.currenttimemillis ())}}, 1000, timeunit.milliseconds);}}
Threadpoolexecutor:
To create a threadpoolexecutor parameter:
corepoolsize: The number of threads that are saved in the pool, including idle threads.
maximumpoolsize: The maximum number of threads allowed in the pool.
KeepAliveTime: When the number of threads is greater than the core, this is the maximum time to wait for a new task before terminating the extra idle thread.
Unit: The time unit of the KeepAliveTime parameter.
workQueue: The queue used to hold the task before execution. This queue keeps only the runnable tasks that are submitted by the Execute method.
threadfactory: The factory that is used when the execution program creates a new thread.
Handler: The handler that is used when execution is blocked because it exceeds the thread range and queue capacity.
Java thread pool Introduction and use