Executors創建線程池的幾種方式以及使用


Java通過Executors提供四種線程池,分別為:
 
1.newCachedThreadPool創建一個可緩存線程池,如果線程池長度超過處理需要,可靈活回收空閑線程,若無可回收,則新建線程。
 
2.newFixedThreadPool 創建一個定長線程池,可控制線程最大並發數,超出的線程會在隊列中等待。
3.newScheduledThreadPool 創建一個定長線程池,支持定時及周期性任務執行。
 
4.newSingleThreadExecutor 創建一個單線程化的線程池,它只會用唯一的工作線程來執行任務,保證所有任務按照指定順序(FIFO, LIFO, 優先級)執行。
 
 
直接上代碼:
 
 1 import lombok.experimental.Delegate;
 2 
 3 import java.util.concurrent.ExecutorService;
 4 import java.util.concurrent.Executors;
 5 
 6 public class ThreadUtil {
 7 
 8     //維護一個單例線程
 9     private static final ThreadUtil threadUtil = new ThreadUtil();
10     
11     // 代理模式  這樣可以直接調用父類中的方法
12     // public interface ExecutorService extends Executor
13     //public interface Executor {
14 
15     /**
16      * Executes the given command at some time in the future.  The command
17      * may execute in a new thread, in a pooled thread, or in the calling
18      * thread, at the discretion of the {@code Executor} implementation.
19      *
20      * @param command the runnable task
21      * @throws RejectedExecutionException if this task cannot be
22      * accepted for execution
23      * @throws NullPointerException if command is null
24      */
25     void execute(Runnable command);
26     }
27     
28     // 1.采用newCachedThreadPool創建線程池
29     @Delegate 
30     public ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
31     
32     //2.采用newFixedThreadPool創建線程池
33     @Delegate
34     public ExecutorService service = Executors.newFixedThreadPool(3);
35     
36     //3.采用newScheduledThreadPool 創建一個定長線程池 支持定時及周期性任務執行。
37     // 使用方法: ThreadUtil.getInstance().schedule(new TestThread(),3, TimeUnit.SECONDS);
38     @Delegate
39     public ScheduledExecutorService newScheduledThreadPool = Executors.newScheduledThreadPool(2);
40     
41 
42     //4.采用newSingleThreadExecutor 創建一個單線程化的線程池
43     @Delegate
44     public ExecutorService newSingleThreadExecutor = Executors.newSingleThreadExecutor();
45 
46     public static ThreadUtil getInstance() {
47         return threadUtil;
48     }
49 
50 }
 1 @Override
 2 public String sendMsg() throws Exception {
 3     
 4     //把業務內容放在類中
 5     ThreadUtil.getInstance().execute(new TestThread());
 6     
 7     //或者這樣直接寫業務內容
 8     ThreadUtil.getInstance().execute( () -> {
 9 
10             System.out.println("222");
11 
12              // 打印線程的內存地址
13              System.out.println(System.identityHashCode(Thread.currentThread()));
14 
15              System.out.println(Thread.currentThread().getName());
16         }
17     );
18     return "ok";
19 }
20 
21 private class TestThread implements Runnable{
22 
23     @Override
24     public void run() {
25         System.out.println("111");
26 
27         System.out.println(Thread.currentThread().getName());
28 
29         System.out.println(System.identityHashCode(Thread.currentThread()));
30     }
31 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM