Java多線程系列七——ExecutorService

來源:互聯網
上載者:User

標籤:大於   介面實現   builder   oid   mina   java多線程   long   imp   tostring   

java.util.concurrent.ExecutorService介面提供了許多線程管理的方法

Method 說明
shutdown 拒絕接收新的任務,待已提交的任務執行後關閉,且宿主線程不阻塞,若需要阻塞可藉助awaitTermination實現
shutdownNow 停止所有正在執行的任務,掛起未執行的任務並關閉,且宿主線程不阻塞,若需要阻塞可藉助awaitTermination實現
awaitTermination 當發生shutdown時,阻塞宿主線程直到約定的時間已過或者所有任務完成
submit 提交任務Callable/Runnable,可利用Future的get()方法使宿主線程阻塞直到任務結束後返回結果

有了以上方法,便可以基於此介面實現線程池的各種功能(例如java.util.concurrent.ThreadPoolExecutor/java.util.concurrent.ScheduledThreadPoolExecutor),以java.util.concurrent.ThreadPoolExecutor為例,其參數的詳解

Name Type 說明
corePoolSize int 線程池中最小的線程數
maximumPoolSize int 線程池中最大的線程數
keepAliveTime long 線程空閑時間,若線程數大於corePoolSize,空閑時間超過該值的線程將被終止回收
unit TimeUnit keepAliveTime的時間單位
workQueue BlockingQueue<Runnable> 已提交但未執行的任務隊列
threadFactory ThreadFactory 建立新線程的工廠
handler RejectedExecutionHandler 當線程池或隊列達到上限拒絕新任務拋出異常時的處理類

同時,java.util.concurrent.Executors類提供了基於java.util.concurrent.ThreadPoolExecutor類的工具方法,常用方法有

Method 說明
newFixedThreadPool 線程池中含固定數量的線程
newSingleThreadExecutor 線程池中僅含一個背景工作執行緒
newCachedThreadPool 按需建立線程,若線程池中無可用線程,則建立新的線程並加入,直到線程數達到上限值(Integer.MAX_VALUE)

測試代碼如下

import java.util.Date;import java.util.concurrent.Callable;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;import java.util.concurrent.TimeUnit;import org.junit.Assert;import org.junit.Test;/** * @Description: 測試ExecutorService */public class ThreadExecutorServiceTest {    private static final String THIS_IS_SHUTDOWN_WITH_AWAIT_TERMINATION = "This is shutdownWithAwaitTermination";    private static final int RESULT = 111;    private static boolean submitRunnable() throws InterruptedException, ExecutionException {        ExecutorService executorService = Executors.newSingleThreadExecutor();        Future<?> future = executorService.submit(new Runnable() {            @Override            public void run() {                System.out.println("This is submitRunnable");            }        });        return future.get() == null;    }    private static Integer submitRunnableWithResult() throws InterruptedException, ExecutionException {        ExecutorService executorService = Executors.newSingleThreadExecutor();        Future<Integer> future = executorService.submit(new Runnable() {            @Override            public void run() {                System.out.println("This is submitRunnableWithResult");            }        }, RESULT);        return future.get();    }    private static Integer submitBlockCallable() throws InterruptedException, ExecutionException {        ExecutorService executorService = Executors.newFixedThreadPool(1);        Future<Integer> future = executorService.submit(new Callable<Integer>() {            @Override            public Integer call() throws Exception {                System.out.println("This is submitBlockCallable");                return RESULT;            }        });        return future.get();// 阻塞    }    private static boolean submitNonBlockCallable() throws InterruptedException, ExecutionException {        ExecutorService executorService = Executors.newFixedThreadPool(1);        Future<Integer> future = executorService.submit(new Callable<Integer>() {            @Override            public Integer call() throws Exception {                System.out.println("This is submitNonBlockCallable");                return RESULT;            }        });        while (!future.isDone()) {// 非阻塞            System.out.println(new Date());        }        return future.isDone();    }    private static String shutdown() throws InterruptedException, ExecutionException {        ExecutorService executorService = Executors.newFixedThreadPool(1);        final StringBuilder sb = new StringBuilder();        executorService.submit(new Callable<Integer>() {            @Override            public Integer call() throws Exception {                Thread.sleep(10000);                sb.append("This is shutdown");                return RESULT;            }        });        executorService.shutdown();        return sb.toString();    }    private static String shutdownWithAwaitTermination() throws InterruptedException, ExecutionException {        ExecutorService executorService = Executors.newFixedThreadPool(1);        final StringBuilder sb = new StringBuilder();        executorService.submit(new Callable<Integer>() {            @Override            public Integer call() throws Exception {                Thread.sleep(10000);                sb.append(THIS_IS_SHUTDOWN_WITH_AWAIT_TERMINATION);                return RESULT;            }        });        executorService.shutdown();        executorService.awaitTermination(Integer.MAX_VALUE, TimeUnit.DAYS);        return sb.toString();    }    @Test    public void test() throws InterruptedException, ExecutionException {        Assert.assertTrue(submitRunnable());        Assert.assertEquals(RESULT, submitRunnableWithResult().intValue());        Assert.assertEquals(RESULT, submitBlockCallable().intValue());        Assert.assertTrue(submitNonBlockCallable());        Assert.assertTrue(shutdown().isEmpty());        Assert.assertEquals(THIS_IS_SHUTDOWN_WITH_AWAIT_TERMINATION, shutdownWithAwaitTermination());    }}

 

Java多線程系列七——ExecutorService

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.