This article can be used as a learning record for the Zhang Xiaoxiang-java multi-threading and concurrency library advanced applications video.
Why do I need to write concurrency before concurrent pool?
New Thread (New Runnable () {public void run{ //... }}). Start ();
There's no problem, why do you need a concurrent pool?
Let's take Tomcat to give an example. Every time we make a request to the server, Tomcat has to separate a thread to handle our business. So, every time we send a request, Tomcat starts a thread like the one above, and when the Run method is done, the thread dies. If this is the case, with 100 requests, Tomcat will have to produce 100 threads, then 100 threads will die, and the next request, Tomcat will have to create another thread. (There is still time spent creating a thread.) )
This is the case, Tomcat is like a hotel foreman, she has 10 waiters, each time a customer, from the 10 waiter to find a person to communicate with customers. When the waiter and the customer Exchange finished, the service back to their original 10 places, waiting for her own next customer. If the hotel is flooded with 15 customers at the same time. So that is 10 attendants at the same time, of course, there are 5 customers no one to entertain, how to do? Waiting for a waiter, waiting for the front of a busy, and then to greet the 5 blocked customers.
The 10 waiters can be understood as a "service pool."
That said, the need for the thread pool to be there should be understood.
The thread pool after Java5 Java5 is mainly referred to as
Java.util.concurrent Interface Executorservice
It is generally used
Java.util.concurrent class executors to produce this category.
Let's look at different ways of producing.
1
Executorservice ThreadPool = Executors.newfixedthreadpool (3);
In this thread pool, there are only 3 threads.
2
Executorservice ThreadPool = Executors.newcachedthreadpool ();
The number of threads constructor is dynamically allocated based on the number of tasks.
3
Executorservice ThreadPool = Executors.newsinglethreadexecutor ();
Line constructor There is only one thread, and if the thread that is currently performing the task is interrupted, the pool creates a new thread instead of it itself.
4
Scheduledexecutorservice ThreadPool = Executors.newscheduledthreadpool ();
Creates a thread pool that can schedule a command to run after a given delay or to execute periodically.
Newfixedthreadpool
Package Thread;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;public class ThreadPoolTest2 {public static void main (string[] args) {Executorservice ThreadPool = Executors.newfixedthread Pool (3); for (int i = 1; I <= 5; i++) {final int task = i; Threadpool.execute (New Runnable () {@Override public void run () { T j = 1; J <= 5; J + +) {try {thread.sleep (20); } catch (Interruptedexception e) {e.printstacktrace (); } System.out.println (Thread.CurrentThread (). GetName () + "is looping of "+ j +" for task of "+ task); } } }); } try {thread.sleep (2000); } catch (InterruptedException e) {e.printstacktrace (); } System.out.println ("All of the ten tasks have committed!"); Here is the possibility to close all threads that are possible above the task has not been completed,//So above the main thread to sleep 2 seconds to ensure that the task is completed Threadpool.shutdownnow (); }}
Look at the results
Pool-1-thread-1 is looping of 1 for task of 1
Pool-1-thread-2 is looping of 1 for task of 2
Pool-1-thread-3 is looping of 1 for task of 3
Pool-1-thread-3 is looping of 2 for task of 3
Pool-1-thread-2 is looping of 2 for task of 2
Pool-1-thread-1 is looping of 2 for task of 1
Pool-1-thread-1 is looping of 3 for task of 1
Pool-1-thread-2 is looping of 3 for task of 2
Pool-1-thread-3 is looping of 3 for task of 3
Pool-1-thread-3 is looping of 4 for task of 3
Pool-1-thread-2 is looping of 4 for task of 2
Pool-1-thread-1 is looping of 4 for task of 1
Pool-1-thread-1 is looping of 5 for task of 1
Pool-1-thread-2 is looping of 5 for task of 2
Pool-1-thread-3 is looping of 5 for task of 3
Pool-1-thread-1 is looping of 1 for task of 4
Pool-1-thread-2 is looping of 1 for task of 5
Pool-1-thread-1 is looping of 2 for task of 4
Pool-1-thread-2 is looping of 2 for task of 5
Pool-1-thread-2 is looping of 3 for task of 5
Pool-1-thread-1 is looping of 3 for task of 4
Pool-1-thread-1 is looping of 4 for task of 4
Pool-1-thread-2 is looping of 4 for task of 5
Pool-1-thread-2 is looping of 5 for task of 5
Pool-1-thread-1 is looping of 5 for task of 4
All of the tasks have committed!
It is the task Of1 2 3 after the completion of 4 5. Why? Because there are only three threads in the pool.
NewcachedthreadpoolWe replace the code that generates the thread pool with the following:
Executorservice ThreadPool = Executors.newcachedthreadpool ();
Results
Pool-1-thread-1 is looping of 1 for task of 1
Pool-1-thread-2 is looping of 1 for task of 2
Pool-1-thread-3 is looping of 1 for task of 3
Pool-1-thread-4 is looping of 1 for task of 4
Pool-1-thread-5 is looping of 1 for task of 5
Pool-1-thread-5 is looping of 2 for task of 5
Pool-1-thread-4 is looping of 2 for task of 4
Pool-1-thread-3 is looping of 2 for task of 3
Pool-1-thread-2 is looping of 2 for task of 2
Pool-1-thread-1 is looping of 2 for task of 1
Pool-1-thread-1 is looping of 3 for task of 1
Pool-1-thread-2 is looping of 3 for task of 2
Pool-1-thread-3 is looping of 3 for task of 3
Pool-1-thread-4 is looping of 3 for task of 4
Pool-1-thread-5 is looping of 3 for task of 5
Pool-1-thread-5 is looping of 4 for task of 5
Pool-1-thread-4 is looping of 4 for task of 4
Pool-1-thread-3 is looping of 4 for task of 3
Pool-1-thread-2 is looping of 4 for task of 2
Pool-1-thread-1 is looping of 4 for task of 1
Pool-1-thread-5 is looping of 5 for task of 5
Pool-1-thread-4 is looping of 5 for task of 4
Pool-1-thread-3 is looping of 5 for task of 3
Pool-1-thread-2 is looping of 5 for task of 2
Pool-1-thread-1 is looping of 5 for task of 1
All of the tasks have committed!
You see, 5 quests are started. Because the line constructor dynamically to add threads.
NewsinglethreadexecutorWe replace the code that generates the thread pool with the following:
Executorservice ThreadPool = Executors.newsinglethreadexecutor ();
Pool-1-thread-1 is looping of 1 for task of 1
Pool-1-thread-1 is looping of 2 for task of 1
Pool-1-thread-1 is looping of 3 for task of 1
Pool-1-thread-1 is looping of 4 for task of 1
Pool-1-thread-1 is looping of 5 for task of 1
Pool-1-thread-1 is looping of 1 for task of 2
Pool-1-thread-1 is looping of 2 for task of 2
Pool-1-thread-1 is looping of 3 for task of 2
Pool-1-thread-1 is looping of 4 for task of 2
Pool-1-thread-1 is looping of 5 for task of 2
Pool-1-thread-1 is looping of 1 for task of 3
Pool-1-thread-1 is looping of 2 for task of 3
Pool-1-thread-1 is looping of 3 for task of 3
Pool-1-thread-1 is looping of 4 for task of 3
Pool-1-thread-1 is looping of 5 for task of 3
Pool-1-thread-1 is looping of 1 for task of 4
Pool-1-thread-1 is looping of 2 for task of 4
Pool-1-thread-1 is looping of 3 for task of 4
Pool-1-thread-1 is looping of 4 for task of 4
Pool-1-thread-1 is looping of 5 for task of 4
Pool-1-thread-1 is looping of 1 for task of 5
Pool-1-thread-1 is looping of 2 for task of 5
Pool-1-thread-1 is looping of 3 for task of 5
Pool-1-thread-1 is looping of 4 for task of 5
Pool-1-thread-1 is looping of 5 for task of 5
All of the tasks have committed!
There is only one thread inside, which naturally appears to be executed sequentially.
Another thing about this "if the thread that is currently performing the task is interrupted, the pool will create a new thread for itself instead."
I don't know the exact example.
Newscheduledthreadpool
Scheduledexecutorservice Schedulepool = Executors.newscheduledthreadpool (1); Import Java.util.concurrent.ScheduledExecutorService; Import Java.util.concurrent.TimeUnit; public class Threadpooltest {public static void main (string[] args) {Scheduledexecutorservice sche Dulepool = Executors.newscheduledthreadpool (1); 5 Seconds to execute task schedulepool.schedule (new Runnable () {public void run () {S YSTEM.OUT.PRINTLN ("Explosion"); }}, 5, timeunit.seconds); Perform the task after 5 seconds and execute the schedulepool.scheduleatfixedrate (new Runnable () {@Override once every 2 seconds public void Run () {System.out.println ("explode"); }}, 5, 2, timeunit.seconds); try {thread.sleep (10*1000); } catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktraCE (); } schedulepool.shutdown (); } }
For a fourth example, see
http://blog.csdn.net/dlf123321/article/details/42741743
Traditional timers
Resources
http://blog.csdn.net/ghsau/article/details/7443324
Concurrent Pools after Java5