1. Executors
The Executors class can be regarded as a "tool class". Citing the introduction in the JDK1.6 API:
Factory and practical methods of the Executor, ExecutorService, ScheduledExecutorService, ThreadFactory, and Callable classes defined in this package. This class supports various methods:
(1) Create and return the method of setting the ExecutorService with commonly used configuration strings.
(2) Create and return the method of setting the ScheduledExecutorService with commonly used configuration strings.
(3) Create and return the "wrapped" ExecutorService method, which disables reconfiguration by making implementation-specific methods inaccessible.
(4) Create and return a method of ThreadFactory, which can set the newly created thread to a known state.
(5) Create and return a non-closed Callable method, so that it can be used in execution methods that require Callable.
Through this class, you can obtain multiple instances of thread pools, such as calling newSingleThreadExecutor() to obtain a single-thread ExecutorService, calling newFixedThreadPool() to obtain an ExecutorService of a fixed-size thread pool, etc. There are more things you can do when you get the ExecutorService. The easiest thing is to use it to execute Runnable objects, or you can execute some objects that implement Callable<T>. There is no return value using Thread's start() method. If the method executed by the thread has a return value, it would be better to use ExecutorService. You can choose submit(), invokeAll() or invokeAny() and select the appropriate method according to the specific situation.
Some of the methods provided in this class are:
1.1 public static ExecutorService newCachedThreadPool()
Create a thread pool that creates new threads as needed, but will reuse them when previously constructed threads are available. These thread pools usually improve program performance for programs that perform many short-term asynchronous tasks.
1.2 public static ExecutorService newFixedThreadPool(int nThreads)
Create a thread pool with a fixed number of reusable threads to run these threads in a shared unbounded queue.
1.3 public static ExecutorService newSingleThreadExecutor()
Create an Executor that uses a single worker thread to run the thread in an unbounded queue.
All three methods can be used with instances of interface ThreadFactory. And return an instance of the ExecutorService interface.
2. Interface ThreadFactory
Create new thread objects as needed. Using thread factories no longer requires manual writing of new Thread calls, allowing applications to use special thread subclasses, properties, etc.
The simplest implementation of this interface is:
class SimpleThreadFactory implements ThreadFactory { public Thread newThread(Runnable r) { return new Thread(r); } } 3. Interface ExecutorService
This interface provides a method to manage termination.
4. Create a standard thread pool startup thread
4.1 Provide a simple thread that implements the Runnable interface
MyThread.java
package com.zj.concurrency.executors; public class MyThread implements Runnable { private int count = 1, number; public MyThread(int num) { number = num; System.out.println("Create Thread-" + number); } public void run() { while (true) { System.out.println("Thread-" + number + " run " + count+" time(s)"); if (++count == 3) return; } }} This thread will print out the corresponding creation and execution information.
4.2 Start thread using CachedThreadPool
CachedThreadPool.java
package com.zj.concurrency.executors;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors; public class CachedThreadPool { public static void main(String[] args) { ExecutorService exec = Executors.newCachedThreadPool(); for (int i = 0; i < 5; i++) exec.execute(new MyThread(i)); exec.shutdown(); }} result:
Create Thread-0Create Thread-1Create Thread-2Create Thread-3Thread-0 run 1 time(s)Thread-0 run 2 time(s)Thread-1 run 1 time(s)Thread-1 run 2 time(s)Thread-2 run 1 time(s)Thread-2 run 2 time(s)Create Thread-4Thread-4 run 1 time(s)Thread-4 run 2 time(s)Thread-3 run 1 time(s)Thread-3 run 2 time(s)
4.3 Start thread using FixedThreadPool
FixedThreadPool.javapackage com.zj.concurrency.executors;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors; public class FixedThreadPool { public static void main(String[] args) { ExecutorService exec = Executors.newFixedThreadPool(2); for (int i = 0; i < 5; i++) exec.execute(new MyThread(i)); exec.shutdown(); }} result:
Create Thread-0Create Thread-1Create Thread-2Create Thread-3Create Thread-4Thread-0 run 1 time(s)Thread-0 run 2 time(s)Thread-2 run 1 time(s)Thread-2 run 2 time(s)Thread-3 run 1 time(s)Thread-3 run 2 time(s)Thread-4 run 1 time(s)Thread-4 run 2 time(s)Thread-1 run 1 time(s)Thread-1 run 2 time(s)
4.4 Start thread using SingleThreadExecutor
SingleThreadExecutor.java
package com.zj.concurrency.executors;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors; public class SingleThreadExecutor { public static void main(String[] args) { ExecutorService exec = Executors.newSingleThreadExecutor(); for (int i = 0; i < 5; i++) exec.execute(new MyThread(i)); exec.shutdown(); }} result:
Create Thread-0Create Thread-1Create Thread-2Create Thread-3Create Thread-4Thread-0 run 1 time(s)Thread-0 run 2 time(s)Thread-1 run 1 time(s)Thread-1 run 2 time(s)Thread-2 run 1 time(s)Thread-2 run 2 time(s)Thread-3 run 1 time(s)Thread-3 run 2 time(s)Thread-4 run 1 time(s)Thread-4 run 2 time(s)
5. Cooperate with the use of ThreadFactory interface
We are trying to add daemon and priority attribute settings to the thread.
5.1 Set background thread properties
DaemonThreadFactory.java
package com.zj.concurrency.executors.factory;import java.util.concurrent.ThreadFactory; public class DaemonThreadFactory implements ThreadFactory { public Thread newThread(Runnable r) { Thread t = new Thread(r); t.setDaemon(true); return t; }}
5.2 Set priority attributes
MaxPriorityThreadFactory.java
package com.zj.concurrency.executors.factory;import java.util.concurrent.ThreadFactory; public class MaxPriorityThreadFactory implements ThreadFactory { public Thread newThread(Runnable r) { Thread t = new Thread(r); t.setPriority(Thread.MAX_PRIORITY); return t; }} Minimum PriorityMinPriorityThreadFactory.java
package com.zj.concurrency.executors.factory;import java.util.concurrent.ThreadFactory; public class MinPriorityThreadFactory implements ThreadFactory { public Thread newThread(Runnable r) { Thread t = new Thread(r); t.setPriority(Thread.MIN_PRIORITY); return t; }}
5.3 Start thread with attribute settings
ExecFromFactory.java
package com.zj.concurrency.executors;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import com.zj.concurrency.executors.factory.DaemonThreadFactory;import com.zj.concurrency.executors.factory.MaxPriorityThreadFactory;import com.zj.concurrency.executors.factory.MinPriorityThreadFactory; public class ExecFromFactory { public static void main(String[] args) throws Exception { ExecutorService defaultExec = Executors.newCachedThreadPool(); ExecutorService daemonExec = Executors .newCachedThreadPool(new DaemonThreadFactory()); ExecutorService maxPriorityExec = Executors .newCachedThreadPool(new MaxPriorityThreadFactory()); ExecutorService minPriorityExec = Executors .newCachedThreadPool(new MinPriorityThreadFactory()); for (int i = 0; i < 10; i++) daemonExec.execute(new MyThread(i)); for (int i = 10; i < 20; i++) if (i == 10) maxPriorityExec.execute(new MyThread(i)); else if (i == 11) minPriorityExec.execute(new MyThread(i)); else defaultExec.execute(new MyThread(i)); }} result:
Create Thread-0Create Thread-1Create Thread-2Create Thread-3Thread-0 run 1 time(s)Thread-0 run 2 time(s)Thread-1 run 1 time(s)Thread-1 run 2 time(s)Thread-2 run 1 time(s)Thread-2 run 2 time(s)Create Thread-4Thread-4 run 1 time(s)Thread-4 run 2 time(s)Create Thread-5Thread-5 run 1 time(s)Thread-5 run 2 time(s)Create Thread-6Create Thread-7Thread-7 run 1 time(s)Thread-7 run 2 time(s)Create Thread-8Thread-8 run 1 time(s)Thread-8 run 2 time(s)Create Thread-9Create Thread-10Thread-10 run 1 time(s)Thread-10 run 2 time(s)Create Thread-11Thread-9 run 1 time(s)Thread-9 run 2 time(s)Thread-6 run 1 time(s)Thread-6 run 2 time(s)Thread-3 run 1 time(s)Thread-3 run 2 time(s)Create Thread-12Create Thread-13Create Thread-14Thread-12 run 1 time(s)Thread-12 run 2 time(s)Thread-13 run 1 time(s)Thread-13 run 2 time(s)Create Thread-15Thread-15 run 1 time(s)Thread-15 run 2 time(s)Create Thread-16Thread-16 run 1 time(s)Thread-16 run 2 time(s)Create Thread-17Create Thread-18Create Thread-19Thread-14 run 1 time(s)Thread-14 run 2 time(s)Thread-17 run 1 time(s)Thread-17 run 2 time(s)Thread-18 run 1 time(s)Thread-18 run 2 time(s)Thread-19 run 1 time(s)Thread-19 run 2 time(s)Thread-11 run 1 time(s)Thread-11 run 2 time(s)