Common thread pool


import
java.util.concurrent.ExecutorService; 4 import java.util.concurrent.Executors; 5 6 public class TestThreadPoolExecutor { 7 public static void main(String[] args) { 8 //创建一个单线程化的线程池 9 ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(); 10 for (int i = 0; i < 10; i++) { 11 final int index = i; 12 singleThreadExecutor.execute(newThe Runnable () { 13 is public void RUN () { 14 the try { 15 // results are sequentially output, corresponding to the order of execution of each task 16 System.out.println (Thread.currentThread (). GetName () + " is being performed, the printing values are: "+ index); . 17 the Thread.sleep (1000 ); 18 is} the catch (InterruptedException E) { . 19 e.printStackTrace (); 20 is } 21 is } 22 is }); 23 is } 24 } 25 }
// create a single-threaded thread pool Executors.newSingleThreadExecutor ();
 // create a cache thread pool Executors.newCachedThreadPool ();
// create a reusable fixed number of thread pool Executors.newFixedThreadPool (3);
 // create a fixed-size thread pool to support regular and periodic task execution - delayed execution Executors.newScheduledThreadPool (5);

Guess you like

Origin www.cnblogs.com/JunLoveHua/p/11720733.html