executorservice

Override interrupt method for thread in threadpool

家住魔仙堡 提交于 2019-12-09 13:28:01
问题 Say I have this: class Queue { private static ExecutorService executor = Executors.newFixedThreadPool(1); public void use(Runnable r){ Queue.executor.execute(r); } } my question is - how can I define the thread that's used in the pool, specifically would like to override the interrupt method on thread(s) in the pool: @Override public void interrupt() { synchronized(x){ isInterrupted = true; super.interrupt(); } } 回答1: Define how threads for the pool are created by specifying a ThreadFactory.

How to use ExecutorService to poll until a result arrives

一个人想着一个人 提交于 2019-12-09 06:47:56
问题 I have a scenario where I have to poll a remote server checking if a task has completed. Once it has, I make a different call to retrieve the result. I originally figured I should use a SingleThreadScheduledExecutor with scheduleWithFixedDelay for polling: ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); ScheduledFuture future = executor.scheduleWithFixedDelay(() -> poll(jobId), 0, 10, TimeUnit.SECONDS); public void poll(String jobId) { boolean jobDone =

Java Executor Service Thread Pool [closed]

吃可爱长大的小学妹 提交于 2019-12-09 06:38:19
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . If I create a fixed size thread pool with 10 threads in java using Executor framework: private final ExecutorService pool; pool = Executors.newFixedThreadPool(10); and then try to submit more than 10 tasks (say

Abort countDownLatch.await() after time out

江枫思渺然 提交于 2019-12-09 03:09:12
问题 I am using an ExecutorService to implement a 3-thread pool, and CountDownLatch to monitor the completion of all threads, for further processing. ExecutorService threadExecutor = Executors.newFixedThreadPool(3); CountDownLatch countDownLatch = new CountDownLatch(3); AuthorisationHistoryTask task1 = new AuthorisationHistoryTask(commonDataThread, countDownLatch ); PreAuthHistoryTask task2 = new PreAuthHistoryTask(userID,sessionID, commonDataThread, countDownLatch ); SettlementHistTask task4 =

How to check if all tasks running on ExecutorService are completed

大兔子大兔子 提交于 2019-12-08 22:49:28
问题 I'v got ConcurrentLinkedDeque which I'm using for synchronic push/pop elements, and I'v got some async tasks which are taking one element from stack and if this element has neighbors It's pushing it to stack. Example code: private ConcurrentLinkedDeque<Item> stack = new ConcurrentLinkedDeque<>(); private ExecutorService exec = Executors.newFixedThreadPool(5); while ((item = stack.pollFirst()) != null) { if (item == null) { } else { Runnable worker = new Solider(this, item); exec.execute

What is terminating my Java ExecutorService

爱⌒轻易说出口 提交于 2019-12-08 20:09:09
问题 I originally saw this issue with a more complex subclass of ThreadPoolExecutor , but I have simplified so now contains not much more than some additional debugging, and still get the same problem. import com.jthink.songkong.cmdline.SongKong; import com.jthink.songkong.ui.MainWindow; import com.jthink.songkong.util.SongKongThreadFactory; import java.util.concurrent.*; import java.util.logging.Level; public class TimeoutThreadPoolExecutor extends ThreadPoolExecutor { /** * Uses the default

Multiple newSingleThreadExecutor vs. newFixedThreadPool of ExecutorService

主宰稳场 提交于 2019-12-08 19:36:25
问题 In my application I have 4 distinct processes, which run permanently with some small pauses. The current version of code executes each process in a separate old-school thread: Thread nlpAnalyzer = new Thread(() -> { // infine lop for auto restore in case of crash //noinspection InfiniteLoopStatement while (true) { try { // this method should run permanently, pauses implemented internally NLPAnalyzer.analyzeNLP(dbCollection); } catch (Exception e) { e.printStackTrace(); } } }); nlpAnalyzer

Java Thread Pool ExecutorService: Threads execution order

旧城冷巷雨未停 提交于 2019-12-08 15:02:27
问题 As we create a Thread pool using Java's Executor service and submit threads to this thread pool, what is the order in which those threads get executed? I want to ensure that threads submitted first, execute first. For example, in the code below, I want first 5 threads to get executed first, followed by the next 5 threads and so on... // Create a thread pool of 5 threads. ScheduledExecutorService exService = Executors.newScheduledThreadPool(5, new ModifiedThreadFactory("ReadThreadPool")); //

java executorservice persist queue

纵饮孤独 提交于 2019-12-08 03:45:18
问题 I implemented the Spring-TaskExecutor (which is the equivalent of the JDK 1.5's Executor.) to process notifications notifications receiving from external systems. Interface with only one method: public interface AsynchronousService { void executeAsynchronously(Runnable task); } and the corresponding implementation: public class AsynchronousServiceImpl implements AsynchronousService { private Executor taskExecutor; @Override public void executeAsynchronously(Runnable task) { taskExecutor

Java: ExecutorService with Callables: invokeAll() and future.get() - results in correct order?

你说的曾经没有我的故事 提交于 2019-12-07 23:13:58
问题 Im using the ExecutorService in Java to invoke Threads with invokeAll() . After, I get the result set with future.get() . Its really important that I receive the results in the same order I created the threads. Here is a snippet: try { final List threads = new ArrayList(); // create threads for (String name : collection) { final CallObject object = new CallObject(name); threads.add(object); } // start all Threads results = pool.invokeAll(threads, 3, TimeUnit.SECONDS); for (Future<String>