Which is created in the thread pool thread, use the thread pool may well improve performance, the program will pass a task thread pool, thread pool will start a thread to perform this task, after the end of execution, the thread does not die, and is back again in the thread pool becomes idle, waiting for the next task.
4 kinds of thread pools are created using Executors:
- Executors.newCacheThreadPool()
Create a new thread on demand, first check the thread pool has no previously established, and if so, on reuse. If not, build a new thread to join the pool, typically used to perform a number of tasks very short lifetime
public class ThreadPoolExecutorTest { public static void main (String [] args) { // Create a thread pool ExecutorService cachedThreadPool = Executors.newCachedThreadPool (); for ( int I = 0; I <10; I ++ ) { the Thread.sleep ( 1000) ; // every second to create a thread cachedThreadPool.execute ( new new Runnable () { public void RUN () { // print thread information cache is being executed System.out.println (Thread.currentThread () getName () +. "it is executed" ); } }); } } }
- Executors.newFixedThreadPool(int n)
When you create a thread pool, thread pool threads that can be executed concurrently fixed, a thread After execution of the current mandate of the thread can be reused to perform another task, n thread pool threads are occupied, the other tasks in accordance with the task of submitting order waiting in the queue
public class ThreadPoolExecutorTest { public static void main (String [] args) { // create a reusable fixed number of thread pool ExecutorService FixedThreadPool Executors.newFixedThreadPool = (. 3 ); for ( int I = 0; I <10; I ++ ) { fixedThreadPool.execute ( new new Runnable () { // very powerful anonymous inner class public void RUN () { the try { // print thread information cache is being executed System.out.println (Thread.currentThread (). getName ( ) + "being executed" ); Thread.sleep ( 2000); // every 2 seconds alternately print the name of the three threads } the catch (InterruptedException E) { e.printStackTrace (); } } }); } } }
- Executors.newSingleThreadExecutor()
Only single-threaded create a thread pool, use it only to perform tasks unique thread, if the thread because the only abnormal termination, then there will be a new thread to replace it, this thread pool to ensure the implementation of all tasks in accordance with the order the task of submitting the order execution
public class TestThreadPoolExecutor { public static void main(String[] args) { //创建一个单线程化的线程池 ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(); for (int i = 0; i < 10; i++) { /*index为不可变量,每一次for循环后本次final变量出了这个块的作用域范围就 会失效,下次循环的时候重建一个不同域的final变量,故不同域对应的线程使用 不同的index*/ final int index = i; singleThreadExecutor.execute(new Runnable() { public void run() { try { //结果依次输出,相当于顺序执行各个任务 System.out.println(Thread.currentThread().getName()+"正在被执 行,打印的值是:"+index); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }); } } }
- Executors.newScheduledThreadPool(int n)
创建一个定长线程池,线程池里面的任务执行又分两种:定时或者周期性执行
- 定时执行
public class ThreadPoolExecutorTest { public static void main(String[] args) { //创建一个定长线程池,支持定时及周期性任务执行——延迟执行 ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5); //延迟1秒执行 scheduledThreadPool.schedule(new Runnable() { public void run() { System.out.println("延迟1秒执行"); } }, 1, TimeUnit.SECONDS);//1秒后执行一次即over } }
- 周期性执行
public class ThreadPoolExecutorTest { public static void main(String[] args) { //创建一个定长线程池,支持定时及周期性任务执行——定期执行 ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5); /*延迟1秒后执行该任务,以后每隔3秒再次执行该任务,每隔3秒若上次任务已完成则 启动本次任务,否则等上次任务结束后才启动本次任务(尽管已经到了3秒)*/ scheduledThreadPool.scheduleAtFixedRate(new Runnable() { public void run() { System.out.println("延迟1秒后再每3秒执行一次"); } }, 1, 3, TimeUnit.SECONDS); } }