| 1. newsinglethreadexecutor
Creates a single threaded pool of threads. This thread pool has only one thread at work, which is equivalent to single-threaded serial execution of all tasks. If this unique thread ends because of an exception, a new thread will replace it. This thread pool guarantees that the order in which all tasks are executed is performed in the order in which the tasks are submitted.
2.Newfixedthreadpool
Creates a fixed-size thread pool. Each time a task is committed, a thread is created until the thread reaches the maximum size of the threads pool. Once the maximum size of the thread pool is reached, the thread pool will be replenished with a new thread if it ends up executing an exception.
3. Newcachedthreadpool
Creates a cacheable pool of threads. If the size of the thread pool exceeds the thread that is required to process the task,
Then a partially idle (60 second non-performing) thread is reclaimed, and when the number of tasks increases, the thread pool can intelligently add new threads to handle the task. This thread pool does not limit the size of the thread pool, and the thread pool size is entirely dependent on the maximum thread size that the operating system (or JVM) can create.
4.Newscheduledthreadpool
Create a thread pool of unlimited size. This thread pool supports the need to schedule and periodically perform tasks.
All of the above methods are implemented
The signature of Threadpoolexecutor 's Complete construction method is:threadpoolexecutor (int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) .
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 units 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 the executor uses when it creates a new thread.
Handler -the handler that is used when execution is blocked because the thread range and queue capacity are exceeded.
Threadpoolexecutor is the underlying implementation of the Executors class.
If you are running fewer threads than if you are running a thread that is equal to or more than corepoolsize, executor always prefers to join the request to the queue without adding a new thread .
If the request cannot be joined to a queue, a new thread is created unless the thread is created that exceeds the
Create a thread pool and execute an example:
//Create a thread pool that can reuse a fixed number of threadsExecutorservice pool = Executors.newcachedthreadpool ();//created implements the Runnable interface object, the thread object of course also implements the Runnable interfaceThread t1 = new MyThread (1);//Put the thread into the pool for execution
Pool.execute (t1);
//close thread pool
Pool.shutdown ();
Schedulewithfixeddelay is literally understood to be a fixed delay (time) to perform the thread task, which is actually not the execution time of the pipeline task, and each time the task is completed and then deferred for a fixed time before the next time. (The next time the thread finishes executing)
Schedulefixedrate , is to perform the thread task at a fixed frequency, the meaning of the fixed frequency is that the fixed time may not be set enough to complete the thread task, but it does not matter, to achieve the set delay time will be executed next. (The next time the thread has not finished executing)
|