executorservice

Optimizing parallel processing of many files

情到浓时终转凉″ 提交于 2019-12-19 09:38:11
问题 I have a piece of program processing a lot of files, where for each files two things needs to be done: First, some piece of the file is read and processed, and then the resulting MyFileData gets stored. The first part can be parallelized, the second can not. Doing everything sequentially is very slow, as the CPU has to wait for the disk, then it works a bit, and then it issues another request, and waits again... I did the following class MyCallable implements Callable<MyFileData> { MyCallable

Optimizing parallel processing of many files

非 Y 不嫁゛ 提交于 2019-12-19 09:38:01
问题 I have a piece of program processing a lot of files, where for each files two things needs to be done: First, some piece of the file is read and processed, and then the resulting MyFileData gets stored. The first part can be parallelized, the second can not. Doing everything sequentially is very slow, as the CPU has to wait for the disk, then it works a bit, and then it issues another request, and waits again... I did the following class MyCallable implements Callable<MyFileData> { MyCallable

Cancelling scheduled executor

限于喜欢 提交于 2019-12-19 07:54:42
问题 I currently have a scheduled executor which sends out a message after a delay like this: executor.schedule(new Runnable() { public void run() { emitter.emit( message ); } }, delay, TimeUnit.MILLISECONDS); I need to have another thread which will listen for a cancel message which will send a different message and I need to stop the above message from sending. If no cancel message is received the above sends as normal. Whats the best way to do this? Thanks. 回答1: This can be done by using the

How to execute multiple queries in parallel instead of sequentially?

馋奶兔 提交于 2019-12-19 04:37:46
问题 I am querying all my 10 tables to get the user id from them and loading all the user id's into HashSet so that I can have unique user id. As of now it is sequentially. We go to one table and extract all the user_id from it and load it in hash set and then second and third table and keep going. private Set<String> getRandomUsers() { Set<String> userList = new HashSet<String>(); // is there any way to make this parallel? for (int table = 0; table < 10; table++) { String sql = "select * from

Using the non final loop variable inside a lambda expression

ぐ巨炮叔叔 提交于 2019-12-18 19:08:38
问题 I'm trying to execute 100 tasks all in parallel via executors and runnable, the task needs to use the loop variable: for (int i = 0; i < 100; i++) { executor.execute(() -> { doSomething(String.format("Need task number %d done", i)); } }); } I get a squiggly under 'i' saying - Variable used in lambda expression should be effectively final. A loop variable, as far as I'm aware, cannot be made final or effectively final, since it is being changed with each iteration. I found a simple workaround,

Is thread starvation deadlock happening here in the code?

孤者浪人 提交于 2019-12-18 17:31:33
问题 //code taken from java concurrency in practice package net.jcip.examples; import java.util.concurrent.*; public class ThreadDeadlock { ExecutorService exec = Executors.newSingleThreadExecutor(); public class LoadFileTask implements Callable<String> { private final String fileName; public LoadFileTask(String fileName) { this.fileName = fileName; } public String call() throws Exception { // Here's where we would actually read the file return ""; } } public class RenderPageTask implements

Does an ExecutorService get garbage collected when out of scope?

天涯浪子 提交于 2019-12-18 14:13:10
问题 I'm asking this question because I am creating a lot of executor services and while I may already have a memory leak somewhere that needs to be investigated, I think a recent change to the following code actually worsened it, hence I am trying to confirm what is going on: @FunctionalInterface public interface BaseConsumer extends Consumer<Path> { @Override default void accept(final Path path) { String name = path.getFileName().toString(); ExecutorService service = Executors

Capturing executor for current thread

和自甴很熟 提交于 2019-12-18 12:03:42
问题 I'm using ListenableFuture from Guava, and one nice thing about them is that one pass Executor to the Futures.addCallback method, that is, ask to execute the callback on a given thread/executor. In my Android application, I want to be able to start the asynchronous execution based on ListenableFuture in the UI thread, and schedule a callback which is also executed also on the UI thread. Therefore, I'd like to somehow submit the UI thread executor to the Futures.addCallback method mentioned

Capturing executor for current thread

自古美人都是妖i 提交于 2019-12-18 12:02:23
问题 I'm using ListenableFuture from Guava, and one nice thing about them is that one pass Executor to the Futures.addCallback method, that is, ask to execute the callback on a given thread/executor. In my Android application, I want to be able to start the asynchronous execution based on ListenableFuture in the UI thread, and schedule a callback which is also executed also on the UI thread. Therefore, I'd like to somehow submit the UI thread executor to the Futures.addCallback method mentioned

How to stop next thread from running in a ScheduledThreadPoolExecutor

家住魔仙堡 提交于 2019-12-18 09:41:00
问题 I have a ScheduledThreadPoolExecutor which has one thread and runs for every 30 seconds. Now, if the current executing thread throws some exception, then I need to make sure that the next thread do not run and the the ScheduledThreadPoolExecutor is down. How do I achieve this? 回答1: As a clean way, you can simply use a static accessed class to set/check the execution availability. import java.util.concurrent.atomic.AtomicBoolean; class ThreadManager { private static AtomicBoolean shouldStop =