Thread pool
The idea of thread pool
The concept of thread pool and the application of executors class
> Create a fixed-size thread pool
> Create a cache thread pool
> Create a single thread pool (how do I restart a thread after it dies?)
Close the thread pool
Comparison of > Shutdown with Shutdownnow
Start the timer with the thread pool
> Call Scheduleexecutorservice's Schedule method, the returned Schedulefuture object can cancel the task.
> Support interval Repetitive task timing mode, does not directly support the decision timing method, need to convert to a relative time mode.
Public classThreadpooltest { Public Static voidMain (string[] args) {//Executorservice ThreadPool = Executors.newfixedthreadpool (3);//Create a fixed-size thread pool, with 3 threads in the threads pools that can serve at the same time//The number of threads in the pool of cache thread pools is dynamically changing, and when all threads are in service state, there are tasks that need to be serviced, automatically adding a thread to service//when the task is completed, the thread is idle for a period of time, and the destroyed thread is automatically reclaimed after time-out//Executorservice ThreadPool = Executors.newcachedthreadpool (); //Create a thread pool with only one threads, and when the thread hangs, it can automatically generate a thread instead of//can solve a lot of people on the Internet asked the question (how to implement the thread dead after restarting?)Executorservice ThreadPool =Executors.newsinglethreadexecutor (); for(inti = 0;i<10;i++) {//throw 10 tasks into the thread pool and be serviced by 3, other threads queued Final intTask =i; Threadpool.execute (NewRunnable () {//threw a task into the thread pool@Override Public voidrun () { for(intj = 0;j<10;j++) {System.out.println (Thread.CurrentThread (). GetName ()+ "is loop of" + j + "The task of" +task); } } }); } System.out.println ("All of the tasks have committed"); //The above code does not end after execution, there are 3 threads in the thread pool that persist, so the program does not end//You can use Threadpool.shutdown ()Threadpool.shutdown ();//when thread pool threads complete all tasks, all threads are idle, all threads are exhausted, the program ends//Threadpool.shutdownnow (); //kill all the threads in the pool immediately, whether or not the task is done }}
Start the timer with the thread pool
Executors.newscheduledthreadpool (3). Schedule ( new Runnable () { @Override Public void run () { System.out.println ("bombing!" ); } }, timeunit.seconds);
You can also add a fixed frequency in the above code
//The fixed frequency is triggered after 10 seconds, and is executed once every two secondsExecutors.newscheduledthreadpool (3). Scheduleatfixedrate (NewRunnable () {@Override Public voidrun () {System.out.println ("Bombing!"); } }, 10, 2, Timeunit.seconds); //the Java API does not provide a trigger at a point in time when using Scheduleatfixedrate, but the API hints can be calculated to get the triggered event point//For example, to schedule at a certain the future date,//You can Use:schedule (task, Date.gettime ()-System.currenttimemillis (), timeunit.milliseconds).
Java Multi-Threading and concurrency Library advanced applications-thread pool