First: Single threaded thread pool This thread pool has only one thread working, 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.
Let's use a small example to first create a class in a Java project
publicclass MyThread extends Thread { @Override publicvoidrun() { "正在执行。。。"); }}
Five threads are added to the thread pool in the Main method
ImportCom.lcjl.fragment.LoginFragment;ImportCom.lcjl.utils.FragmentUtils;ImportAndroid.os.Bundle;Importandroid.support.v4.app.FragmentActivity;ImportAndroid.support.v4.app.FragmentTransaction; Public class mytextactivity extends fragmentactivity { PrivateLoginfragment loginfragment;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.main); Fragmenttransaction transaction = Fragmentutils.getinstance (). Myfragmentutils ( This); Loginfragment =NewLoginfragment (); Transaction.add (R.id.rel, loginfragment). commit (); }}
Let's take a look at the effect of our operation
From we can see that only one thread is running
Second: Creating a fixed-size thread pool creates a thread each time a task is committed, 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.
Like the top, we're writing a little demo.
The first is a thread
publicclass MyThread extends Thread { @Override publicvoidrun() { "正在执行。。。"); }}
Implementation code in the Main method
ImportJava.util.concurrent.ExecutorService;ImportJava.util.concurrent.Executors; Public class mainthread { Public Static void Main(string[] args) {//Create a thread pool that can reuse thread pools with fixed threadsExecutorservice pool = executors. Newfixedthreadpool (2);//Create an instance of MythreadThread T1 =NewMyThread (); Thread t2 =NewMyThread (); Thread t3 =NewMyThread (); Thread T4 =NewMyThread (); Thread T5 =NewMyThread ();//Put the thread into the pool for executionPool.execute (t1); Pool.execute (T2); Pool.execute (T3); Pool.execute (T4); Pool.execute (T5);//close thread poolPool.shutdown (); }}
Let's take a look at what we allow two threads to run let's see how it works.
Third: Create a pool of threads that can be cached 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.
Not as much as the top words. Take a look at our implementation code
Implementation code in the thread class
publicclass MyThread extends Thread { @Override publicvoidrun() { "正在执行。。。"); }}
Implementation code in the Main method
ImportJava.util.concurrent.ExecutorService;ImportJava.util.concurrent.Executors; Public class mainthread { Public Static void Main(string[] args) {//Create a thread pool that can reuse thread pools with fixed threads//Executorservice pool = executors. Newsinglethreadexecutor ();Executorservice pool = executors. Newcachedthreadpool ();//Create an instance of Mythread for(inti =0; I < -; i++) {Thread Ithread =NewMyThread (); Pool.execute (Ithread); }//close thread poolPool.shutdown (); }}
Let's take a look at the effects of our operation.
We have 100 threads open in the code, but the biggest of our runs is the 30 threads, which is a cacheable thread. This thread is actually the most practical.
Our last thread pool is described below
Fourth: A thread pool of unlimited size This pooling of threads supports timing and the need to perform tasks periodically.
Here's an example of the implementation code in the Main method
ImportJava.util.concurrent.ScheduledThreadPoolExecutor;ImportJava.util.concurrent.TimeUnit; Public class mainthread { Public Static void Main(string[] args) {Scheduledthreadpoolexecutor exec =NewScheduledthreadpoolexecutor (1); Exec.scheduleatfixedrate (NewRunnable () {//Every time the exception is triggered @Override Public void Run() {//throw new RuntimeException ();System.out.println ("================"); } }, +, the, timeunit.milliseconds); Exec.scheduleatfixedrate (NewRunnable () {//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); }}
Let's look at the effect of the operation
Perform tasks on a timed basis
Four thread pools