栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java线程池并发ExecutorService

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java线程池并发ExecutorService

当我们有任务需要多线程来完成时,将任务(实现Runnable、callable接口、继承Thread类的对象)提交给ExecutorService。

1.创建ExecutorService

public static ExecutorService newFixedThreadPool(int nThreads)
创建固定数目线程的线程池。public static ExecutorService newCachedThreadPool()
创建一个可缓存的线程池,调用execute将重用以前构造的线程(如果线程可用)。如果现有线程没有可用的,则创建一个新线 程并添加到池中。终止并从缓存中移除那些已有 60 秒钟未被使用的线程。public static ExecutorService newSingleThreadExecutor()
创建一个单线程化的Executor。public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)
创建一个支持延迟及周期性的任务执行的线程池,多数情况下可用来替代Timer类。

ExecutorService executorService1 = Executors.newSingleThreadExecutor();  
ExecutorService executorService2 = Executors.newFixedThreadPool(10);  
ExecutorService executorService3 = Executors.newScheduledThreadPool(10);



2.ExecutorService的执行

2.1execute(Runnable)

ExecutorService executorService = Executors.newSingleThreadExecutor();

executorService.execute(new Runnable() {
public void run() {
    System.out.println("Asynchronous task");
}
});

executorService.shutdown();

2.2submit(Runnable)

Future future = executorService.submit(new Runnable() {
public void run() {
    System.out.println("Asynchronous task");
}
});

future.get();  //returns null if the task has finished correctly.

2.3submit(Callable)

Future future = executorService.submit(new Callable(){
public Object call() throws Exception {
    System.out.println("Asynchronous Callable");
    return "Callable Result";
}
});

System.out.println("future.get() = " + future.get());

2.4invokeAny(…)

ExecutorService executorService = Executors.newSingleThreadExecutor();

Set> callables = new HashSet>();

callables.add(new Callable() {
public String call() throws Exception {
    return "Task 1";
}
});
callables.add(new Callable() {
public String call() throws Exception {
    return "Task 2";
}
});
callables.add(new Callable() {
    public String call() throws Exception {
    return "Task 3";
}
});

String result = executorService.invokeAny(callables);
System.out.println("result = " + result);
executorService.shutdown();

2.5invokeAll(…)

ExecutorService executorService = Executors.newSingleThreadExecutor();

Set> callables = new HashSet>();

callables.add(new Callable() {
public String call() throws Exception {
    return "Task 1";
}
});
callables.add(new Callable() {
    public String call() throws Exception {
    return "Task 2";
}
});
callables.add(new Callable() {
public String call() throws Exception {
    return "Task 3";
}
});

List> futures = executorService.invokeAll(callables);

for(Future future : futures){
System.out.println("future.get = " + future.get());
}

executorService.shutdown();

 

3.example

@Configuration
public class ExecutorConfig {

    private static final int QUEUE_SIZE = 100_000;

    @Bean
    public ExecutorService dogTaskExecutor() {
        return ExecutorUtil.genExecutorService("dogTask", 50, QUEUE_SIZE);
    }

    @Bean
    public ExecutorService catBagExecutor() {
        return ExecutorUtil.genExecutorService("cat", 50, QUEUE_SIZE);
    }

    

}

 

public class ExecutorUtil {

    public static ExecutorService genExecutorService(String name, int currency, int queue) {
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat(name + "-%d").build();
        ExecutorService executorService = new ThreadPoolExecutor(currency, currency*2, 60L, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(queue), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
        return TtlExecutors.getTtlExecutorService(executorService);
    }
}

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/720553.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号