1. First knowledge thread pool: According to the system's own environment, effectively limit the number of execution threads, so that the performance of the best. The thread is primarily controlled by the number of threads that are executing, the number of threads is queued, waiting for the task to complete, and the task execution is removed from the front of the queue. 2. Thread pool role: Reduce the number of times a thread is created and destroyed, and each worker thread can use multiple threads that can be adjusted to the system to prevent excessive memory consumption by 3. Common thread pool: ①newsinglethreadexecutor thread pool for individual threads, That is, only one thread is working in the thread pool at a time, single-threaded serial execution task ②newfixedthreadexecutor (n) a fixed number of thread pools, not committing a task is just one threads, until the maximum number of thread pools is reached, Then go back into the wait queue until the previous task is complete before proceeding with ③newcachethreadexecutor (recommended) cache thread pool, when the thread pool size exceeds the threads required to process the task, then a portion of the idle (typically 60 seconds without execution) is reclaimed, and when there is a task , and then intelligently add new threads to execute. ④newschedulethreadexecutor size unlimited thread pool that supports timed and recurring execution Threads 4. Example:
PublicclassmythreadextendsThread {@Override publicvoid run () {System.out.println (Thread.CurrentThread (). GetName ()+ "in-execution ... "); }}①newsinglethreadexecutorpublicclasstestsinglethreadexecutor {publicstaticvoid main (String[] args) {//Create a thread pool that can reuse a fixed number of threadsExecutorservice pool =executors. Newsinglethreadexecutor (); //creates an Runnable interface object that implements theThread TT1 =NewMyThread (); Thread TT2=NewMyThread (); Thread tt3=NewMyThread (); Thread Tt4=NewMyThread (); Thread Tt5=NewMyThread (); //put the thread into the pool and executePool.execute (TT1); Pool.execute (TT2); Pool.execute (TT3); Pool.execute (TT4); Pool.execute (TT5); //ClosePool.shutdown (); }}result:pool-1-thread-1 in execution ... Pool-1-thread-1 in execution ... Pool-1-thread-1 in execution ... Pool-1-thread-1 in execution ... Pool-1-thread-1 in execution ... ②newfixedthreadexecutor (n) publicclass Testfixedthreadpool {publicstaticvoid main (string[] args) {//Create a thread pool that can reuse a fixed number of threadsExecutorservice pool = Executors.newfixedthreadpool (2); //creates an Runnable interface object that implements theThread T1 =NewMyThread (); Thread T2=NewMyThread (); Thread T3=NewMyThread (); Thread T4=NewMyThread (); Thread T5=NewMyThread (); //Putting a thread into the pool for executionPool.execute (t1); Pool.execute (T2); Pool.execute (T3); Pool.execute (T4); Pool.execute (T5); //Close the thread poolPool.shutdown (); }}result:pool-1-thread-1 in execution ... Pool-1-thread-2 in execution ... Pool-1-thread-1 in execution ... Pool-1-thread-2 in execution ... Pool-1-thread-1 in execution ... ③newcachethreadexecutorpublicclass Testcachedthreadpool {publicstaticvoid main (string[] args) {//Create a thread pool that can reuse a fixed number of threadsExecutorservice pool =Executors.newcachedthreadpool (); //creates an Runnable interface object that implements theThread T1 =NewMyThread (); Thread T2=NewMyThread (); Thread T3=NewMyThread (); Thread T4=NewMyThread (); Thread T5=NewMyThread (); //Putting a thread into the pool for executionPool.execute (t1); Pool.execute (T2); Pool.execute (T3); Pool.execute (T4); Pool.execute (T5); //Close the thread poolPool.shutdown (); }}result:pool-1-thread-1 in execution ... Pool-1-thread-2 in execution ... Pool-1-thread-4 in execution ... Pool-1-thread-3 in execution ... Pool-1-thread-5 in execution ...
Understanding and use of thread pool