Overview
First, say a few of the classes that are commonly used in Java connection pooling: Executor,executorservice,scheduledexecutorservice
Executes the Runnable task object that has been committed. This interface provides a mechanism for separating task submissions from task execution.
It is a sub-interface of executor, can terminate the submission of new thread tasks, can Chinese line constructor all existing threads, can also batch submit thread tasks and so on. There are many ways to read the relevant APIs in detail.
Can defer thread task execution
The thread implementations in the case in this article are as follows:
Public classThreaddemoImplementsrunnable{PrivateString ThreadName =NULL; Private BooleanFlag =true; Private intcount; Private intcounter; Private Longsuspend; /*** This is the constructor *@paramThreadName *@paramCount Cycle times *@paramsuspend thread end time, per millisecond*/ PublicThreaddemo (String ThreadName,intCountLongsuspend) { Super(); This. ThreadName =ThreadName; This. Count =count; This. Suspend =suspend; } /*** Run*/@Override Public voidrun () { while(flag) {Try{thread.sleep (suspend); System.out.println (ThreadName+"--------------"+counter); Counter++; if(counter>count) {Flag=false; } } Catch(interruptedexception e) {e.printstacktrace (); } } }}View Codecreate an infinitely large thread pool
Executorservice ExecutorService1 = executors.newcachedthreadpool (); Executorservice1.execute (New Threaddemo ("Thread B-1", 5, ()); Executorservice1.execute (new threaddemo ("Thread B-2", 5, +)); Executorservice1.shutdown (); the Executorservice1.shutdown () function is to refuse to receive other threads, and after the threads of the online constructor are executed, the thread pool is closed.
Create a thread pool of a specific size
Executorservice ExecutorService1 = Executors.newfixedthreadpool (2); Executorservice1.submit (New Threaddemo ("Thread B-1", 5, ()); Executorservice1.submit (new threaddemo ("Thread B-2", 5, +)); Executorservice1.submit (new threaddemo ("Thread B-3", 5, +)); Executorservice1.shutdown (); The size of the thread pool created here is 2, and if the number of threads committed is greater than 2, the excess will wait in the queue
Create a thread pool for single thread execution
Executorservice ExecutorService1 = executors.newsinglethreadexecutor (); Executorservice1.execute (New Threaddemo ("Thread A-1", 5, ()); Executorservice1.execute (new threaddemo ("Thread A-2", 5, +)); Executorservice1.shutdown (); Can commit multiple threads, but only one thread at a time, others waiting in the queue
To create a thread pool that delays the execution of threads
Scheduledexecutorservice ExecutorService1 = Executors.newscheduledthreadpool (2); Executorservice1.schedule ( New threaddemo ("Thread B-1", 5, 1, timeunit.seconds); Executorservice1.schedule (New Threaddemo ("Thread B-2", 5, $), 2, timeunit.seconds); Executorservice1.schedule (new threaddemo ("Thread B-3 ", 5, 3, timeunit.seconds); Executorservice1.shutdown ();
About corepoolsize in executors.newscheduledthreadpool (int corepoolsize): When a task is submitted to the thread pool, the line pool creates a thread to perform the task, Even if other idle basic threads are able to perform new tasks, the thread is created and is no longer created until the number of tasks that need to be performed is greater than the thread pool base size. If the thread pool's Prestartallcorethreads method is called, the thread pool creates and starts all basic threads in advance.
Creating a timed single-thread pool
Scheduledexecutorservice ExecutorService1 = executors.newsinglethreadscheduledexecutor (); Executorservice1.schedule (new threaddemo ("Thread A-1", 5, 1,timeunit.seconds); Executorservice1.schedule (new threaddemo ("Thread A-2", 5, 2,timeunit.seconds); Executorservice1.shutdown ();
What are the methods of each class, look at the specific API, the above is just a syntax example.
Java Concurrency: Executor and connection pooling