I’m trying to make a ScheduledExecutorService where only one task is active at a time and only once a task has finished, the next task will begin its delay with an arbitrary delay amount. As a very simple example of what I mean, take a look at this method. The idea is to schedule 10 Runnables to simulate a countdown
Tag: executorservice
Executor service returning incorrect response
I am creating future list from a list of calls to executor service submit method based on student ID. The response from service is not returning for all studentId’s. It runs for the right number of times but the studentId getting used in service call is either first or last. It is ignoring the middle ones. Please check the code
Callable in ExecutorService with shared data
I have a scenario which I have somehow simulated into the following: I have a list of Callable tasks that are being executed in 4 threads. The execution should return a value(a Map<String, String>). Each task has an item attribute. If for a task I get timeout (I have a Timeout flag in a Map<> that each Callable returns) or
In Java, how to test a private code running in parallel with executor service wrapped in public method
I had an existing non-parallel code that we recently made concurrent by using executor service. Adding concurrency ensured limit the number of requests sent to another API in our scenario. So we are calling an external service and limiting requests, waiting for all requests to complete so as merge the responses later before sending the final response. I am stuck
Graceful shutdown of multithreaded java server using executor service
I refactored a single-threaded server to be capable of multithreading and accepting multiple clients at the same time. To do so, I spawn a new ClientHandler thread for each new client and submit it to an ExecutorService. I want to initiate a server shutdown by entering a new line to System.In. However, I am not able to shut down the
How to run Executor Service on a list and perform some action in Java
I have a list of items which I want to run an executor service to perform some expensive action: Say I have 100 items in the list, I expect the es to performExpensiveAction on all 100 items but it only performs the action on 10 items. Answer You should consider using invokeAll instead shutdown. The shutdown() method will allow previously
How do I pass different value to each thread in ExecutorService?
Lets say I have an array num_array = [0,1,2,3,4…30] and I have a multi-threading function like this I wanted to know how I can pass num_array[0] to thread 1 and num_array[1] to thread 2 … num_array[24] to thread 25 Answer Just loop over the array and pass each runnable the element: Note that you should not assume the order of
Using Java stream forEach() in ScheduledExecutorService freezes
The general idea is to have a Runnable running every 10 seconds in background to check some data and if needed make changes in an object. ScheduledExecutorService is instantiated in method main() and the task is scheduled. Runnable task instantiates Crawler object and starts crawling. Most of the times it runs couple of times with success but when application is
How to use executor service in loop for each iteration?
My aim is to fetch data of batch size 2(say) and for 3(say) batches/pages as mentioned in the above code. I’m trying to give the responsibility for fetching the data and pushing the event for a page to a thread i.e. 1 thread for 1 page. I worked on it and able to write the above logic. But when I’m
Killing threads from completion service?
Problem I am using a completion service and spawning child threads to perform some ETL. As I debug in my IDE and then stop all processes, I notice I still have a bunch of zombie threads killing my CPU. This is due to the fact that I’m not terminating the child threads properly. Minimum Example Thoughts Essentially, I submit my