Netty進階之EventExecutorGroup源碼詳解
前言
EventExecutorGroup繼承了JDK的ScheduledExecutroService,那么它就擁有了執(zhí)行定時任務(wù),執(zhí)行提交的普通任務(wù);
EventExecutorGroup還繼承了JDK的Iterable接口,表示EventExecutorGroup是可遍歷的,它的遍歷對象是EventExecutor;
一、Iterable相關(guān)方法
EventExecutorGroup中有兩個Iterable相關(guān)的方法:
//返回一個被當前EventExecutorGroup管理的EventExecutor對象 EventExecutor next(); //返回一個EventExecutor類型的迭代器 @Override Iterator<EventExecutor> iterator();
二、execute
接口Executor提供了一個提交執(zhí)行任務(wù)的方法execute
//返回一個被當前EventExecutorGroup管理的EventExecutor對象 EventExecutor next(); //返回一個EventExecutor類型的迭代器 @Override Iterator<EventExecutor> iterator();
三、Executor的子接口ExecutorService
public interface ExecutorService extends Executor { /** * 啟動有序關(guān)閉,在關(guān)閉過程中會繼續(xù)執(zhí)行以前提交的任務(wù),但是不接受新的任務(wù) */ void shutdown(); /** * 嘗試停止所有正在執(zhí)行的任務(wù),停止處理等待的任務(wù),并返回一個等待執(zhí)行的任務(wù)列表 */ List<Runnable> shutdownNow(); /** * 如果此執(zhí)行程序已關(guān)閉,則返回true */ boolean isShutdown(); /** * 如果關(guān)閉后所有任務(wù)都已完成,則返回true * 只有首先調(diào)用了shutdown()、shutdownNow()方法才有可能返回true,否則一定為false */ boolean isTerminated(); /** * 阻塞,shutdown后所有任務(wù)執(zhí)行完成、超時、當前線程被終端,那個先發(fā)生那個優(yōu)先; */ boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException; /** * 提交一個帶返回值的執(zhí)行任務(wù),并返回一個Future代表將要返回的任務(wù)結(jié)果 */ <T> Future<T> submit(Callable<T> task); /** * 提交一個可運行任務(wù)以供執(zhí)行,并返回一個表示該任務(wù)的Future。Future的get方法將在成功完成后返回給定的結(jié)果。 */ <T> Future<T> submit(Runnable task, T result); /** * 提交一個可運行任務(wù)以供執(zhí)行,并返回一個表示該任務(wù)的Future。Future的get方法在成功完成后將返回null。 */ Future<?> submit(Runnable task); /** * 執(zhí)行給定的多個任務(wù),返回一個持有執(zhí)行狀態(tài)和結(jié)果的Future列表。 */ <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException; /** * 執(zhí)行給定的多個任務(wù),返回一個持有執(zhí)行狀態(tài)和結(jié)果的Future列表。 * 當所有的任務(wù)完成或超時過期Future#isDone都將返回ture */ <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException; /** * 執(zhí)行給定的多個任務(wù),返回其中一個執(zhí)行成功的結(jié)果 */ <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException; /** * 執(zhí)行給定的多個任務(wù),返回其中一個執(zhí)行成功的結(jié)果 */ <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException; }
下面是一個網(wǎng)絡(luò)服務(wù)案例,其中線程池中的線程為傳入請求提供服務(wù),它使用預(yù)先配置的Executors.newFixedThreadPool工廠方法:
class NetworkService implements Runnable { private final ServerSocket serverSocket; private final ExecutorService pool; public NetworkService(int port, int poolSize) throws IOException { serverSocket = new ServerSocket(port); pool = Executors.newFixedThreadPool(poolSize); } public void run() { // run the service try { for (; ; ) { pool.execute(new Handler(serverSocket.accept())); } } catch (IOException ex) { pool.shutdown(); } } } class Handler implements Runnable { private final Socket socket; Handler(Socket socket) { this.socket = socket; } public void run() { // read and service request on socket } }
下面的方法關(guān)閉ExecutorService分兩個階段,首先通過調(diào)用shutdown方法拒絕新任務(wù)進來,然后調(diào)用shutdownNow,如果有必要取消任何延遲任務(wù):
void shutdownAndAwaitTermination(ExecutorService pool) { pool.shutdown(); // Disable new tasks from being submitted try { // Wait a while for existing tasks to terminate if (!pool.awaitTermination(60, TimeUnit.SECONDS)) { pool.shutdownNow(); // Cancel currently executing tasks // Wait a while for tasks to respond to being cancelled if (!pool.awaitTermination(60, TimeUnit.SECONDS)) System.err.println("Pool did not terminate"); } } catch (InterruptedException ie) { // (Re-)Cancel if current thread also interrupted pool.shutdownNow(); // Preserve interrupt status Thread.currentThread().interrupt(); } }
四、ExecutorService的子接口ScheduledExecutorService
ScheduledExecutorService接口提供了四個新的方法schedule延遲指定的時間執(zhí)行任務(wù),scheduleAtFixedRate方法延遲指定時間、按照固定的時間頻率執(zhí)行任務(wù)。
public interface ScheduledExecutorService extends ExecutorService { /** * 提交一個一次性任務(wù),該任務(wù)在給定延遲后變?yōu)閱⒂脿顟B(tài)。 * * @param command 要執(zhí)行的任務(wù) * @param delay 從現(xiàn)在開始延遲執(zhí)行的時間 * @param unit delay參數(shù)的時間單位 * @return 一個ScheduledFuture,標識任務(wù)的掛起完成,其get方法將在完成時返回null */ public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit); /** * 提交一個帶返回值的一次性任務(wù),該任務(wù)在給定延遲后變?yōu)閱⒂脿顟B(tài) * * @param callable 要執(zhí)行的函數(shù)任務(wù) * @param delay 從現(xiàn)在開始延遲執(zhí)行的時間 * @param unit delay參數(shù)的時間單位 * @param <V> the type of the callable's result * @return 可用于提取結(jié)果或取消的ScheduledFuture */ public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit); /** * 提交一個可執(zhí)行任務(wù),延遲指定時間,然后按照period時間間隔執(zhí)行任務(wù) */ public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit); /** * 提交一個可執(zhí)行任務(wù),延遲指定時間,然后按照period時間間隔執(zhí)行任務(wù) */ public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit); }
五、ScheduledExecutorService的子接口EventExecutorGroup
EventExecutorGroup新增了三個方法:
- isShuttingDown:當且僅當此EventExecutorGroup管理的所有EventExecutor正在正常關(guān)閉或已關(guān)閉時,返回true
- shutdownGracefully:指定了超時時間的優(yōu)雅關(guān)閉方法;
到此這篇關(guān)于Netty進階之EventExecutorGroup源碼詳解的文章就介紹到這了,更多相關(guān)EventExecutorGroup源碼詳解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實現(xiàn)斷點下載服務(wù)端與客戶端的示例代碼
這篇文章主要為大家介紹了如何實現(xiàn)服務(wù)端(Spring Boot)與客戶端(Android)的斷點下載與下載續(xù)傳功能,文中的示例代碼講解詳細,需要的可以參考一下2022-08-08JAVA使用JDBC技術(shù)操作SqlServer數(shù)據(jù)庫實例代碼
本篇文章主要介紹了JAVA使用JDBC技術(shù)操作SqlServer數(shù)據(jù)庫實例代碼,具有一定的參考價值,有興趣的可以了解一下。2017-01-01SpringBoot實現(xiàn)HTTP調(diào)用的7 種方式
本文主要介紹了SpringBoot實現(xiàn)HTTP調(diào)用的7 種方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-04-04Java多線程 ReentrantReadWriteLock原理及實例詳解
這篇文章主要介紹了Java多線程 ReentrantReadWriteLock原理及實例詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09SpringBoot實現(xiàn)發(fā)送短信的示例代碼
這篇文章主要介紹了SpringBoot實現(xiàn)發(fā)送短信的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04