I. Overview
Java. util. concurrent has a very convenient thread pool implementation. The Executor framework provided contains the static factory methods for managing and implementing Runnable tasks. The Executors class provides various types of Executors.
Ii. Instances
Public class MyTask implements Runnable
{
Public void run ()
{
System. out. println (Thread. currentThread (). getName () + "the task is running ");
}
}
① Fixed-size thread pool newFixedThreadPool
Public class FixedThreadPoolTest
{
Public static void main (String [] args)
{
// Create a thread pool with a fixed size of 5
ExecutorService threadPool = Executors. newFixedThreadPool (5 );
// 20 tasks are simulated, and there are only five threads in the thread pool.
For (int I = 0; I <20; I ++)
{
Try
{
// Execute five tasks at a time, and other tasks are queued
Thread. sleep (20 );
ThreadPool.exe cute (new MyTask ());
}
Catch (Exception e)
{
E. printStackTrace ();
}
}
ThreadPool. shutdown ();
}
}
The pool-1-thread-1 task is running.
The pool-1-thread-2 task is running.
The pool-1-thread-3 task is running.
Pool-1-thread-4 task running
Pool-1-thread-5 task running
The pool-1-thread-1 task is running.
The pool-1-thread-2 task is running.
The pool-1-thread-3 task is running.
Pool-1-thread-4 task running
Pool-1-thread-5 task running
The pool-1-thread-1 task is running.
The pool-1-thread-2 task is running.
The pool-1-thread-3 task is running.
Pool-1-thread-4 task running
Pool-1-thread-5 task running
The pool-1-thread-1 task is running.
The pool-1-thread-2 task is running.
The pool-1-thread-3 task is running.
Pool-1-thread-4 task running
Pool-1-thread-5 task running
② There is no limit on the size of the thread pool newCachedThreadPool
Public class CachedThreadPool
{
Public static void main (String [] args)
{
// No limit on the thread pool size
// It will be used again when the previously created thread can be used
ExecutorService threadPool = Executors. newCachedThreadPool ();
For (int I = 0; I <20; I ++)
{
ThreadPool.exe cute (new MyTask ());
}
ThreadPool. shutdown ();
}
}
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-2 task is running.
Pool-1-thread-4 task running
The pool-1-thread-3 task is running.
The pool-1-thread-2 task is running.
The pool-1-thread-1 task is running.
Pool-1-thread-6 task running
Pool-1-thread-4 task running
The pool-1-thread-1 task is running.
The pool-1-thread-3 task is running.
Pool-1-thread-4 task running
The pool-1-thread-1 task is running.
Pool-1-thread-5 task running
The pool-1-thread-7 task is running.
The pool-1-thread-3 task is running.
The pool-1-thread-2 task is running.
Pool-1-thread-8 task running
Pool-1-thread-6 task running
③ NewSingleThreadExecutor
Public class SingleThreadPool
{
Public static void main (String [] args)
{
// Single thread pool
ExecutorService threadPool = Executors. newSingleThreadExecutor ();
For (int I = 0; I <20; I ++)
{
ThreadPool.exe cute (new MyTask ());
}
ThreadPool. shutdown ();
}
} The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
The pool-1-thread-1 task is running.
④ NewScheduledThreadPool
Public class ScheduledPool
{
Public static void main (String [] args)
{
// Scheduled task Thread Pool
ScheduledExecutorService threadPool = Executors. newScheduledThreadPool (3 );
// Run the task three seconds later
ThreadPool. schedule (new MyTask (), 3, TimeUnit. SECONDS );
// Run in five seconds, every two seconds
ThreadPool. scheduleAtFixedRate (new MyTask (), 5, 2, TimeUnit. SECONDS );
}
}