1. 程式人生 > >Java併發程式設計之執行緒池(三)

Java併發程式設計之執行緒池(三)

一.介紹
Java通過Executors提供四種執行緒池,分別為:
(1)newCachedThreadPool:建立一個可快取執行緒池,如果執行緒池長度超過處理需要,可靈活回收空閒執行緒,若無可回收,則新建執行緒。
(2)newFixedThreadPool: 建立一個定長執行緒池,可控制執行緒最大併發數,超出的執行緒會在佇列中等待。
(3)newScheduledThreadPool :建立一個定長執行緒池,支援定時及週期性任務執行。
(4)newSingleThreadExecutor: 建立一個單執行緒化的執行緒池,它只會用唯一的工作執行緒來執行任務,保證所有任務按照指定順序(FIFO, LIFO, 優先順序)執行。
二.基本使用

1.newCachedThreadPool

建立一個可快取執行緒池,應用中存在的執行緒數可以無限大

    public static class TestRunable implements Runnable {
        int index;
        public TestRunable(int i){
            this.index=i;
        }
        @Override
        public void run() {
            System.out.println("我的執行緒"+index+":"+
this.toString()); } } public static void main(String[] args) { ExecutorService newCachedThreadPool = newCachedThreadPool(); for(int i=0;i<10;i++) { newCachedThreadPool.execute(new TestRunable(i)); } }

結果:

我的執行緒0:concurrency.ThreadPoolTest T

e s t R u n a b l e @ 4 a 24 a 59 1 : c o n c u r r e n c y . T h r e a d P o o l T e s t [email protected] 我的執行緒1:concurrency.ThreadPoolTest [email protected]
我的執行緒2:concurrency.ThreadPoolTest T e s t R u n a b l e @ 57 a 0 e 8 e a 3 : c o n c u r r e n c y . T h r e a d P o o l T e s t [email protected] 我的執行緒3:concurrency.ThreadPoolTest [email protected]
我的執行緒4:concurrency.ThreadPoolTest T e s t R u n a b l e @ 797372 a c 5 : c o n c u r r e n c y . T h r e a d P o o l T e s t [email protected] 我的執行緒5:concurrency.ThreadPoolTest [email protected]
我的執行緒6:concurrency.ThreadPoolTest T e s t R u n a b l e @ 68422010 7 : c o n c u r r e n c y . T h r e a d P o o l T e s t [email protected] 我的執行緒7:concurrency.ThreadPoolTest [email protected]
我的執行緒8:concurrency.ThreadPoolTest T e s t R u n a b l e @ 3 b 3595 c 8 9 : c o n c u r r e n c y . T h r e a d P o o l T e s t [email protected] 我的執行緒9:concurrency.ThreadPoolTest [email protected]

2.newFixedThreadPool
 public static class TestRunable implements Runnable {
        int index;
        public TestRunable(int i){
            this.index=i;
        }
        @Override
        public void run() {
            System.out.println("我的執行緒"+index+":"+this.toString()+"當前時間:"+new Date());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        ExecutorService newCachedThreadPool = newFixedThreadPool(3);
        for(int i=0;i<10;i++) {
            newCachedThreadPool.execute(new TestRunable(i));
        }
    }

結果:

我的執行緒1:concurrency.ThreadPoolTest T e s t R u n a b l e @ 2 b 3 b 219 b : W e d N o v 1416 : 37 : 08 C S T 2018 0 : c o n c u r r e n c y . T h r e a d P o o l T e s t [email protected]當前時間:Wed Nov 14 16:37:08 CST 2018 我的執行緒0:concurrency.ThreadPoolTest [email protected]當前時間:Wed Nov 14 16:37:08 CST 2018
我的執行緒2:concurrency.ThreadPoolTest T e s t R u n a b l e @ 53 b 69 d 37 : W e d N o v 1416 : 37 : 08 C S T 2018 3 : c o n c u r r e n c y . T h r e a d P o o l T e s t [email protected]當前時間:Wed Nov 14 16:37:08 CST 2018 我的執行緒3:concurrency.ThreadPoolTest [email protected]當前時間:Wed Nov 14 16:37:09 CST 2018
我的執行緒4:concurrency.ThreadPoolTest T e s t R u n a b l e @ 7067 b 35 a : W e d N o v 1416 : 37 : 09 C S T 2018 5 : c o n c u r r e n c y . T h r e a d P o o l T e s t [email protected]當前時間:Wed Nov 14 16:37:09 CST 2018 我的執行緒5:concurrency.ThreadPoolTest [email protected]當前時間:Wed Nov 14 16:37:09 CST 2018
我的執行緒6:concurrency.ThreadPoolTest T e s t R u n a b l e @ 70339 e e : W e d N o v 1416 : 37 : 10 C S T 2018 7 : c o n c u r r e n c y . T h r e a d P o o l T e s t [email protected]當前時間:Wed Nov 14 16:37:10 CST 2018 我的執行緒7:concurrency.ThreadPoolTest