亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Netty進(jìn)階之EventExecutorGroup源碼詳解

 更新時(shí)間:2023年11月16日 10:44:19   作者:立小研先森  
這篇文章主要介紹了Netty進(jìn)階之EventExecutorGroup源碼詳解,EventExecutorGroup繼承了JDK的ScheduledExecutroService,那么它就擁有了執(zhí)行定時(shí)任務(wù),執(zhí)行提交的普通任務(wù),需要的朋友可以參考下

前言

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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • BaseJDBC和CRUDDAO的寫法實(shí)例代碼

    BaseJDBC和CRUDDAO的寫法實(shí)例代碼

    這篇文章主要介紹了BaseJDBC和CRUDDAO的寫法實(shí)例代碼,代碼注釋十分詳細(xì),具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-09-09
  • Java測(cè)試框架Mockito的簡(jiǎn)明教程

    Java測(cè)試框架Mockito的簡(jiǎn)明教程

    這篇文章主要介紹了Java測(cè)試框架Mockito的簡(jiǎn)明教程,Mock 測(cè)試是單元測(cè)試的重要方法之一。本文介紹了基于 Java 語(yǔ)言的 Mock 測(cè)試框架 – Mockito 的使用。,需要的朋友可以參考下
    2019-06-06
  • 詳解Java中的流程控制

    詳解Java中的流程控制

    今天帶大家復(fù)習(xí)Java基礎(chǔ)知識(shí),文中對(duì)Java流程控制作了非常詳細(xì)的介紹及代碼示例,對(duì)正在學(xué)習(xí)Java的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05
  • 將對(duì)象轉(zhuǎn)化為字符串的java實(shí)例

    將對(duì)象轉(zhuǎn)化為字符串的java實(shí)例

    這篇文章主要介紹了將對(duì)象轉(zhuǎn)化為字符串的java實(shí)例,有需要的朋友可以參考一下
    2013-12-12
  • Java實(shí)現(xiàn)斷點(diǎn)下載服務(wù)端與客戶端的示例代碼

    Java實(shí)現(xiàn)斷點(diǎn)下載服務(wù)端與客戶端的示例代碼

    這篇文章主要為大家介紹了如何實(shí)現(xiàn)服務(wù)端(Spring Boot)與客戶端(Android)的斷點(diǎn)下載與下載續(xù)傳功能,文中的示例代碼講解詳細(xì),需要的可以參考一下
    2022-08-08
  • JAVA使用JDBC技術(shù)操作SqlServer數(shù)據(jù)庫(kù)實(shí)例代碼

    JAVA使用JDBC技術(shù)操作SqlServer數(shù)據(jù)庫(kù)實(shí)例代碼

    本篇文章主要介紹了JAVA使用JDBC技術(shù)操作SqlServer數(shù)據(jù)庫(kù)實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下。
    2017-01-01
  • SpringBoot實(shí)現(xiàn)HTTP調(diào)用的7 種方式

    SpringBoot實(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-04
  • Java多線程 ReentrantReadWriteLock原理及實(shí)例詳解

    Java多線程 ReentrantReadWriteLock原理及實(shí)例詳解

    這篇文章主要介紹了Java多線程 ReentrantReadWriteLock原理及實(shí)例詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • Java 網(wǎng)絡(luò)編程總結(jié)

    Java 網(wǎng)絡(luò)編程總結(jié)

    這篇文章主要給大家分享Java 網(wǎng)絡(luò)編程的一個(gè)總結(jié),說到網(wǎng)絡(luò)編程肯定都會(huì)想到IP地址、端口、通信協(xié)議等一些必不可少的元素,下面來(lái)看看文章的詳細(xì)介紹吧
    2021-11-11
  • SpringBoot實(shí)現(xiàn)發(fā)送短信的示例代碼

    SpringBoot實(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

最新評(píng)論