executorservice

How can I interrupt RestTemplate call as soon as my thread is interrupted?

不羁的心 提交于 2019-11-28 12:29:52
I need to make a library in which I will have synchronous and asynchronous feature. executeSynchronous() - waits until I have a result, returns the result. executeAsynchronous() - returns a Future immediately which can be processed after other things are done, if needed. Core Logic of my Library The customer will use our library and they will call it by passing DataKey builder object. We will then construct a URL by using that DataKey object and make a HTTP client call to that URL by executing it and after we get the response back as a JSON String, we will send that JSON String back to our

How can I terminate Tasks that have timed out in multithreading?

柔情痞子 提交于 2019-11-28 11:18:27
问题 I need to make a library in which I will have synchronous and asynchronous methods in it. executeSynchronous() - waits until I have a result, returns the result. executeAsynchronous() - returns a Future immediately which can be processed after other things are done, if needed. Core Logic of my Library The customer will use our library and they will call it by passing DataKey builder object. We will then construct a URL by using that DataKey object and make a HTTP client call to that URL by

How to stop an executor thread when Tomcat is stopped?

旧巷老猫 提交于 2019-11-28 10:23:21
I am using an executor service by creating a fixed number of threads to do a HTTP GET data retrieval. executorService = ExecutorServiceFactory.getInstance().getExecutorService(Config.getInstance().getNumberOfThreads(), "ThreadExecutor_"+UIDFactory.getInstance().createSessionID()); executorService.execute(new Retrieve(data)); private class Retrieve implements Runnable{ private Vector<String> data; public WADORetrieve(Vector<String> data) { this.data = data; } @Override public void run() { for(int i=0;i<data.length;i++){ fetch(data[i]); } } } When Tomcat is stopped we get this error: SEVERE: The

Using InheritableThreadLocal with ThreadPoolExecutor — or — a ThreadPoolExecutor that doesn't reuse threads

一世执手 提交于 2019-11-28 09:47:15
I am trying to use both InheritableThreadLocal and a ThreadPoolExecutor . This breaks down because ThreadPoolExecutor reuses threads for each pool (it is a pool, after all), meaning the InheritableThreadLocal doesn't work as expected. Now the problem seems obvious to me, but it was particularly snarly to track down. I use InheritableThreadLocal so that each of several top-level processes has its own database connection for itself and any sub-processes it spawns. I don't just use one shared connection pool because each top-level process will do a lot of multi-step work with its connection

Java ExecutorService pause/resume a specific thread

余生长醉 提交于 2019-11-28 08:35:12
Is there a way to use ExecutorService to pause/resume a specific thread? private static ExecutorService threadpool = Executors.newFixedThreadPool(5); Imagine that I want to stop the thread wich as the id=0 (assuming that to each one is assigned an incremental id until the size of the threadpool is reached). After a while, by pressing a button let's say, I want to resume that specific thread and leave all the other threads with their current status, which can be paused or resumed. I have found on Java documentation a uncompleted version of PausableThreadPoolExecutor. But it doesn't suit what I

ScheduledExecutorService with variable delay

痞子三分冷 提交于 2019-11-28 08:17:00
Suppose I have a task that is pulling elements from a java.util.concurrent.BlockingQueue and processing them. public void scheduleTask(int delay, TimeUnit timeUnit) { scheduledExecutorService.scheduleWithFixedDelay(new Task(queue), 0, delay, timeUnit); } How can I schedule / reschedule the task if the frequency can be changed dynamically? The idea is to take a stream of data updates and propagate them in batch to a GUI The user should be able to vary the frequency of updates Brian Agnew I don't think you can change a fixed rate delay. I think you need to use schedule() to perform a one-shot,

Whether to use invokeAll or submit - java Executor service

谁都会走 提交于 2019-11-28 07:43:50
I have a scenario where I have to execute 5 thread asynchronously for the same callable. As far as I understand, there are two options: 1) using submit(Callable) ExecutorService executorService = Executors.newFixedThreadPool(5); List<Future<String>> futures = new ArrayList<>(); for(Callable callableItem: myCallableList){ futures.add(executorService.submit(callableItem)); } 2) using invokeAll(Collections of Callable) ExecutorService executorService = Executors.newFixedThreadPool(5); List<Future<String>> futures = executorService.invokeAll(myCallableList)); What should be the preferred way? Is

Removing all queued tasks of an ThreadPoolExecutor

三世轮回 提交于 2019-11-28 07:19:51
i have this rather simple question about the ThreadPoolExecutor . I have the following situation: I have to consume objects from a queue, create the appropiate worker tasks for them and submit them to the ThreadPoolExecutor. This is quite simple. But within a shutdown scenario many workers may be queued to execution. Since one of those tasks might be running for an hour, and i want a relativly fast graceful shutdown of the application i want to discard all queued tasks from the ThreadPoolExecutor while the already processing tasks should be completed normally. The ThreadPoolExecutor

java Fork/Join pool, ExecutorService and CountDownLatch

ぃ、小莉子 提交于 2019-11-28 07:06:41
We have three different multi threading techniques in java - Fork/Join pool, Executor Service & CountDownLatch Fork/Join pool ( http://www.javacodegeeks.com/2011/02/java-forkjoin-parallel-programming.html ) The Fork/Join framework is designed to make divide-and-conquer algorithms easy to parallelize. That type of algorithms is perfect for problems that can be divided into two or more sub-problems of the same type. They use recursion to break down the problem to simple tasks until these become simple enough to be solved directly. The solutions to the sub-problems are then combined to give a

How is an executor termination recursion in java?

混江龙づ霸主 提交于 2019-11-28 06:31:53
问题 This is a program that reads information site for previous format it uses recursion and executor.It works fine,my problem is to test whether the program is completed and success notification. public class NewClass { static String levels[] = { "div.col-md-9 li a", "div#sidebar ul li a" }; static String links = ""; private void getRecursive(String href, int level, final ExecutorService executor) { if (level > levels.length - 1) { return; } Document doc; try { doc = Jsoup.connect(href).get();