Threads and Thread Pools
1. The role of the thread pool
Limit the number of threads executing in the system
2. Why use a thread pool
2.1. Reduces the number of times a thread is created and destroyed, and each worker thread can be reused to perform multiple tasks.
2.2 You can adjust the number of threads in the thread pool based on your system's ability to withstand
Prevent the server from consuming too much memory and getting down (each thread needs about 1MB of memory, the more threads open, the more memory is consumed, and the last crashes).
3. Thread pool Interface class
3.1 Executorservice: The real thread pool interface.
3.2 Scheduledexecutorservice: Can be similar to Timer/timertask to solve problems that require tasks to be repeatedly executed.
3.3 The default implementation of Threadpoolexecutor:executorservice.
3.4 Scheduledthreadpoolexecutor: Inherit the Threadpoolexecutor Scheduledexecutorservice interface implementation, the class implementation of periodic task scheduling.
4. Thread pool static tool class executors common methods
4.1 Newsinglethreadexecutor
Create a single thread pool with borderless queue linkedblockingqueue. That is, the thread is limited, but the queue is infinite.
4.2 Newfixedthreadpool
Creates a fixed-size thread pool with borderless queue linkedblockingqueue. That is, the thread is limited, but the queue is infinite.
4.3 newcachedthreadpool
Creates a cacheable thread pool with synchronousqueue queues.
4.4 Newscheduledthreadpool
Create a thread pool of unlimited size. This thread pool supports the need to schedule and periodically perform tasks.
5. Thread pool execution strategy and Blockingqueue
5.1 If you run fewer threads than corepoolsize (the minimum number of threads), executor always prefers to add new threads without queuing.
5.2 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.
5.3 If the request cannot be queued, a new thread is created unless the thread is created beyond maximumpoolsize, in which case the task is rejected.
6. Example
public class MyThread extends Thread
{
private String name;
Public MyThread (String name)
{
THIS.name = name;
}
@Override
public void Run ()
{
Try
{
Thread.Sleep (10000);
System.out.println (Thread.CurrentThread (). GetName () +name+ "--executing!");
}
catch (Interruptedexception e)
{
E.printstacktrace ();
}
}
}
6.1 Newsinglethreadexecutor
public class Testsinglethreadexecutor
{
public static void Main (string[] args)
{
Single Thread execution
Executorservice Snglethreadpool = Executors.newsinglethreadexecutor ();
MyThread T1 = new MyThread ("T1");
MyThread t2 = new MyThread ("T2");
MyThread t3 = new MyThread ("T3");
MyThread T4 = new MyThread ("T4");
Snglethreadpool.execute (t1);
Snglethreadpool.execute (T2);
Snglethreadpool.execute (T3);
Snglethreadpool.execute (T4);
Snglethreadpool.shutdown ();
}
}
6.2 Scheduledthreadpoolexecutor
public class Testscheduledthreadpool
{
public static void Main (string[] args)
{
Scheduledthreadpoolexecutor exec = new Scheduledthreadpoolexecutor (1);
Exec.scheduleatfixedrate (New Runnable ()
{//Trigger an exception every once in a while
@Override
public void Run ()
{
System.out.println ("================");
}
}, +, timeunit.milliseconds);
Exec.scheduleatfixedrate (New Runnable ()
{//Print the system time every once in a while to prove that the two do not affect each other
@Override
public void Run ()
{
System.out.println (System.nanotime ());
}
}, +, timeunit.milliseconds);
}
}
6.3 Newfixedthreadpool
public class Testfixedthreadpool
{
public static void Main (string[] args)
{
Multiple thread execution, fixed size thread
Executorservice Fixedthreadpool = Executors.newfixedthreadpool (5);
MyThread T1 = new MyThread ("T1");
MyThread t2 = new MyThread ("T2");
MyThread t3 = new MyThread ("T3");
MyThread T4 = new MyThread ("T4");
Fixedthreadpool.execute (t1);
Fixedthreadpool.execute (T2);
Fixedthreadpool.execute (T3);
Fixedthreadpool.execute (T4);
Fixedthreadpool.shutdown ();
}
}
6.4 Newcachedthreadpool
public class Testcachedthreadpool
{
public static void Main (string[] args)
{
Multiple thread execution, unlimited number of wireless threads. Determined by the CPU of the running environment
Executorservice Cachedthreadpool = Executors.newcachedthreadpool ();
MyThread T1 = new MyThread ("T1");
MyThread t2 = new MyThread ("T2");
MyThread t3 = new MyThread ("T3");
MyThread T4 = new MyThread ("T4");
Cachedthreadpool.execute (t1);
Cachedthreadpool.execute (T2);
Cachedthreadpool.execute (T3);
Cachedthreadpool.execute (T4);
Cachedthreadpool.shutdown ();
}
}
6.6 Threadpoolexecutor
public class Testthreadpoolexecutor
{
public static void Main (string[] args)
{
Create a thread pool from the constructor
Threadpoolexecutor pool = new Threadpoolexecutor (2, 3, Timeunit.seconds, New linkedblockingqueue<runnable> ()) ;
MyThread T1 = new MyThread ("T1");
MyThread t2 = new MyThread ("T2");
MyThread t3 = new MyThread ("T3");
MyThread T4 = new MyThread ("T4");
Pool.execute (t1);
Pool.execute (T2);
Pool.execute (T3);
Pool.execute (T4);
Pool.shutdown ();
}
}
One of the Java thread leaks