Netty進(jìn)階之EventExecutorGroup源碼詳解
前言
EventExecutorGroup繼承了JDK的ScheduledExecutroService,那么它就擁有了執(zhí)行定時(shí)任務(wù),執(zhí)行提交的普通任務(wù);
EventExecutorGroup還繼承了JDK的Iterable接口,表示EventExecutorGroup是可遍歷的,它的遍歷對(duì)象是EventExecutor;
一、Iterable相關(guān)方法
EventExecutorGroup中有兩個(gè)Iterable相關(guān)的方法:
//返回一個(gè)被當(dāng)前EventExecutorGroup管理的EventExecutor對(duì)象 EventExecutor next(); //返回一個(gè)EventExecutor類型的迭代器 @Override Iterator<EventExecutor> iterator();
二、execute
接口Executor提供了一個(gè)提交執(zhí)行任務(wù)的方法execute
//返回一個(gè)被當(dāng)前EventExecutorGroup管理的EventExecutor對(duì)象 EventExecutor next(); //返回一個(gè)EventExecutor類型的迭代器 @Override Iterator<EventExecutor> iterator();
三、Executor的子接口ExecutorService
public interface ExecutorService extends Executor { /** * 啟動(dòng)有序關(guān)閉,在關(guān)閉過程中會(huì)繼續(xù)執(zhí)行以前提交的任務(wù),但是不接受新的任務(wù) */ void shutdown(); /** * 嘗試停止所有正在執(zhí)行的任務(wù),停止處理等待的任務(wù),并返回一個(gè)等待執(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í)行完成、超時(shí)、當(dāng)前線程被終端,那個(gè)先發(fā)生那個(gè)優(yōu)先; */ boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException; /** * 提交一個(gè)帶返回值的執(zhí)行任務(wù),并返回一個(gè)Future代表將要返回的任務(wù)結(jié)果 */ <T> Future<T> submit(Callable<T> task); /** * 提交一個(gè)可運(yùn)行任務(wù)以供執(zhí)行,并返回一個(gè)表示該任務(wù)的Future。Future的get方法將在成功完成后返回給定的結(jié)果。 */ <T> Future<T> submit(Runnable task, T result); /** * 提交一個(gè)可運(yùn)行任務(wù)以供執(zhí)行,并返回一個(gè)表示該任務(wù)的Future。Future的get方法在成功完成后將返回null。 */ Future<?> submit(Runnable task); /** * 執(zhí)行給定的多個(gè)任務(wù),返回一個(gè)持有執(zhí)行狀態(tài)和結(jié)果的Future列表。 */ <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException; /** * 執(zhí)行給定的多個(gè)任務(wù),返回一個(gè)持有執(zhí)行狀態(tài)和結(jié)果的Future列表。 * 當(dāng)所有的任務(wù)完成或超時(shí)過期Future#isDone都將返回ture */ <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException; /** * 執(zhí)行給定的多個(gè)任務(wù),返回其中一個(gè)執(zhí)行成功的結(jié)果 */ <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException; /** * 執(zhí)行給定的多個(gè)任務(wù),返回其中一個(gè)執(zhí)行成功的結(jié)果 */ <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException; }
下面是一個(gè)網(wǎng)絡(luò)服務(wù)案例,其中線程池中的線程為傳入請(qǐng)求提供服務(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分兩個(gè)階段,首先通過調(diào)用shutdown方法拒絕新任務(wù)進(jìn)來(lái),然后調(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接口提供了四個(gè)新的方法schedule延遲指定的時(shí)間執(zhí)行任務(wù),scheduleAtFixedRate方法延遲指定時(shí)間、按照固定的時(shí)間頻率執(zhí)行任務(wù)。
public interface ScheduledExecutorService extends ExecutorService { /** * 提交一個(gè)一次性任務(wù),該任務(wù)在給定延遲后變?yōu)閱⒂脿顟B(tài)。 * * @param command 要執(zhí)行的任務(wù) * @param delay 從現(xiàn)在開始延遲執(zhí)行的時(shí)間 * @param unit delay參數(shù)的時(shí)間單位 * @return 一個(gè)ScheduledFuture,標(biāo)識(shí)任務(wù)的掛起完成,其get方法將在完成時(shí)返回null */ public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit); /** * 提交一個(gè)帶返回值的一次性任務(wù),該任務(wù)在給定延遲后變?yōu)閱⒂脿顟B(tài) * * @param callable 要執(zhí)行的函數(shù)任務(wù) * @param delay 從現(xiàn)在開始延遲執(zhí)行的時(shí)間 * @param unit delay參數(shù)的時(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); /** * 提交一個(gè)可執(zhí)行任務(wù),延遲指定時(shí)間,然后按照period時(shí)間間隔執(zhí)行任務(wù) */ public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit); /** * 提交一個(gè)可執(zhí)行任務(wù),延遲指定時(shí)間,然后按照period時(shí)間間隔執(zhí)行任務(wù) */ public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit); }
五、ScheduledExecutorService的子接口EventExecutorGroup
EventExecutorGroup新增了三個(gè)方法:
- isShuttingDown:當(dāng)且僅當(dāng)此EventExecutorGroup管理的所有EventExecutor正在正常關(guān)閉或已關(guān)閉時(shí),返回true
- shutdownGracefully:指定了超時(shí)時(shí)間的優(yōu)雅關(guān)閉方法;
到此這篇關(guān)于Netty進(jìn)階之EventExecutorGroup源碼詳解的文章就介紹到這了,更多相關(guān)EventExecutorGroup源碼詳解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Netty進(jìn)階之ChannelPoolMap源碼解析
- Netty中ChannelPoolHandler調(diào)用處理程序詳解
- Java的Netty進(jìn)階之Future和Promise詳解
- 深入理解Netty?FastThreadLocal優(yōu)缺點(diǎn)及實(shí)現(xiàn)邏輯
- SpringBoot 整合 Netty 多端口監(jiān)聽的操作方法
- SpringBoot整合Netty的流程步驟
- Springboot+WebSocket+Netty實(shí)現(xiàn)在線聊天/群聊系統(tǒng)
- spring?cloud?gateway中netty線程池小優(yōu)化
相關(guān)文章
Java測(cè)試框架Mockito的簡(jiǎn)明教程
這篇文章主要介紹了Java測(cè)試框架Mockito的簡(jiǎn)明教程,Mock 測(cè)試是單元測(cè)試的重要方法之一。本文介紹了基于 Java 語(yǔ)言的 Mock 測(cè)試框架 – Mockito 的使用。,需要的朋友可以參考下2019-06-06將對(duì)象轉(zhuǎn)化為字符串的java實(shí)例
這篇文章主要介紹了將對(duì)象轉(zhuǎn)化為字符串的java實(shí)例,有需要的朋友可以參考一下2013-12-12Java實(shí)現(xiàn)斷點(diǎn)下載服務(wù)端與客戶端的示例代碼
這篇文章主要為大家介紹了如何實(shí)現(xiàn)服務(wù)端(Spring Boot)與客戶端(Android)的斷點(diǎn)下載與下載續(xù)傳功能,文中的示例代碼講解詳細(xì),需要的可以參考一下2022-08-08JAVA使用JDBC技術(shù)操作SqlServer數(shù)據(jù)庫(kù)實(shí)例代碼
本篇文章主要介紹了JAVA使用JDBC技術(shù)操作SqlServer數(shù)據(jù)庫(kù)實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01SpringBoot實(shí)現(xiàn)HTTP調(diào)用的7 種方式
本文主要介紹了SpringBoot實(shí)現(xiàn)HTTP調(diào)用的7 種方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-04-04Java多線程 ReentrantReadWriteLock原理及實(shí)例詳解
這篇文章主要介紹了Java多線程 ReentrantReadWriteLock原理及實(shí)例詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09SpringBoot實(shí)現(xiàn)發(fā)送短信的示例代碼
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)發(fā)送短信的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04