executorservice

Executor框架+实例

南笙酒味 提交于 2020-03-01 09:30:29
Executor框架 是指java 5中引入的一系列并发库中与executor相关的一些功能类,其中包括线程池,Executor,Executors,ExecutorService,CompletionService,Future,Callable等。 Executor框架简介 Executor框架描述 Executor框架 是指java 5中引入的一系列并发库中与executor相关的一些功能类,其中包括线程池,Executor,Executors,ExecutorService,CompletionService,Future,Callable等。 运用该框架能够很好的 将任务分成一个个的子任务 ,使并发编程变得方便。 Executor相关类图 该框架的类图(方法并没有都表示出来)如下: 创建线程池类别 创建线程池 Executors类,提供了一系列工厂方法用于创先线程池,返回的线程池都实现了ExecutorService接口。 public static ExecutorService newFixedThreadPool(int nThreads) 创建固定数目线程的线程池 public static ExecutorService newCachedThreadPool() 创建一个可缓存的线程池 创建一个可缓存的线程池,调用execute 将重用以前构造的线程

java并发库之Executors常用的创建ExecutorService的几个方法说明

孤人 提交于 2020-02-29 20:00:36
一、线程池的创建 我们可以通过ThreadPoolExecutor来创建一个线程池。 new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, milliseconds,runnableTaskQueue, handler); 创建一个线程池需要输入几个参数: corePoolSize(线程池的基本大小):当提交一个任务到线程池时,线程池会创建一个线程来执行任务,即使其他空闲的基本线程能够执行新任务也会创建线程,等到需要执行的任务数大于线程池基本大小时就不再创建。如果调用了线程池的prestartAllCoreThreads方法,线程池会提前创建并启动所有基本线程。 runnableTaskQueue(任务队列):用于保存等待执行的任务的阻塞队列。 可以选择以下几个阻塞队列。 ArrayBlockingQueue:是一个基于数组结构的有界阻塞队列,此队列按 FIFO(先进先出)原则对元素进行排序。 LinkedBlockingQueue:一个基于链表结构的阻塞队列,此队列按FIFO (先进先出) 排序元素,吞吐量通常要高于ArrayBlockingQueue。静态工厂方法Executors.newFixedThreadPool()使用了这个队列。 SynchronousQueue

How to call same method of a different class in multithreaded way

怎甘沉沦 提交于 2020-02-28 19:38:09
问题 I have a method named process in two of my Classes, lets say CLASS-A and CLASS-B . Now in the below loop, I am calling process method of both of my classes sequentially meaning one by one and it works fine but that is the not the way I am looking for. for (ModuleRegistration.ModulesHolderEntry entry : ModuleRegistration.getInstance()) { final Map<String, String> response = entry.getPlugin().process(outputs); // write to database System.out.println(response); } Is there any way, I can call the

How to call same method of a different class in multithreaded way

喜你入骨 提交于 2020-02-28 19:34:06
问题 I have a method named process in two of my Classes, lets say CLASS-A and CLASS-B . Now in the below loop, I am calling process method of both of my classes sequentially meaning one by one and it works fine but that is the not the way I am looking for. for (ModuleRegistration.ModulesHolderEntry entry : ModuleRegistration.getInstance()) { final Map<String, String> response = entry.getPlugin().process(outputs); // write to database System.out.println(response); } Is there any way, I can call the

多线程IO操作(扫描文件夹并计算总大小)

走远了吗. 提交于 2020-02-28 11:55:02
场景为,给到一个硬盘上文件或文件夹,(当然文件夹时,多线程的优势才能越发体现出来),得到该文件或文件夹的大小和计算该结果所需要的时间。 首先是单线程下的例子,这个可难不倒大家,代码如下: public class TotalFileSizeSequential { private long getTotalSizeOfFilesInDir(final File file) { if (file.isFile()) return file.length(); final File[] children = file.listFiles(); long total = 0; if (children != null) for(final File child : children) total += getTotalSizeOfFilesInDir(child); return total; } public static void main(final String[] args) { final long start = System.nanoTime(); final long total = new TotalFileSizeSequential() .getTotalSizeOfFilesInDir(new File("D:/idea_ws")); final long

ScheduledExecutorService: when shutdown should be invoked?

主宰稳场 提交于 2020-02-03 04:00:29
问题 I use ScheduledExecutorService in my application. I need to use it from time to time in certain Utility class to run scheduled threads. Is it a good design to hold ScheduledExecutorService in static field? Is it a must to invoke ScheduledExecutorService.shutdown() in such case? What is the risk if I do not invoke shutdown? That's what I thought to do: private static ScheduledExecutorService exec = Executors.newScheduledThreadPool(5); public void scheduleTask(String name) { Future<?> future =

How to use custom executor in Akka dispatcher

为君一笑 提交于 2020-01-30 05:27:11
问题 I am trying to use a custom executor for a dispatcher in Akka. Specifically, I want to wrap an existing executor with my own (logging, debugging, etc.). I've looked at relevant parts of documentation: The default dispatcher can be configured, and is by default a Dispatcher with a “fork-join-executor”, which gives excellent performance in most cases. and specify using “executor” using “fork-join-executor”, “thread-pool-executor” or the FQCN of an akka.dispatcher.ExecutorServiceConfigurator (I

Runnable locked (park) using ExecutorService and BlockingQueue

痞子三分冷 提交于 2020-01-25 04:18:06
问题 Note: I understand the rules site, but I can't to put all code (complex/large code). I wish I was more specific, but I don't know what part to extract and show. Before you close this question: Obviously, I am willing to refine my question if someone tells me where to look (technical detail). I made a video in order to show my issue. Even to formulate the question, I made a diagram to show the situation. My program has a JTree , showing the relations between Worker . I have a diagram

invokeAll() is not willing to accept a Collection<Callable<T>>

别来无恙 提交于 2020-01-22 18:51:47
问题 I fail to understand why this code won't compile ExecutorService executor = new ScheduledThreadPoolExecutor(threads); class DocFeeder implements Callable<Boolean> {....} ... List<DocFeeder> list = new LinkedList<DocFeeder>(); list.add(new DocFeeder(1)); ... executor.invokeAll(list); The error msg is: The method invokeAll(Collection<Callable<T>>) in the type ExecutorService is not applicable for the arguments (List<DocFeeder>) list is a Collection of DocFeeder , which implements Callable

How can I set Socket write timout in java?

别说谁变了你拦得住时间么 提交于 2020-01-20 07:02:29
问题 I have a problem with handling socket in java. I am running a TCP server with multiple client connections. For performance reason, I used a simple thread pool to handle packets. Please see code below public enum LazyWorkCenter { instance; LazyWorkCenter() { lazyWorker = new NamedThreadPoolExecutor(3,3, 0L,TimeUnit.MILLISECONDS, "LazyWorker"); } private ExecutorService lazyWorker ; public void executeLazy(Runnable lazyTask) { lazyWorker.execute(lazyTask); } } public class TcpServerForClient {