executorservice

Execution context without daemon threads for futures

匆匆过客 提交于 2019-12-18 06:58:14
问题 I am having trouble with the JVM immediately exiting using various new applications I wrote which spawn threads through the Scala 2.10 Futures + Promises framework. It seems that at least with the default execution context, even if I'm using blocking, e.g. future { blocking { /* work */ }} no non-daemon thread is launched, and therefore the JVM thinks it can immediately quit. A stupid work around is to launch a dummy Thread instance which is just waiting, but then I also need to make sure

Java: Force stopping of ExecutorService threads

﹥>﹥吖頭↗ 提交于 2019-12-18 05:02:36
问题 My code: String[] torrentFiles = new File("/root/torrents/").list(); if(torrentFiles.length == 0 || torrentFiles == null) { System.exit(0); } ex = Executors.newFixedThreadPool(3); for(String torrentFile : torrentFiles) { ex.submit(new DownloadTorrent("/root/torrents/" + torrentFile)); } ex.shutdown(); try { ex.awaitTermination(30, TimeUnit.MINUTES); } catch(InterruptedException ex1) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex1); } But sometimes torrent downloading

Java: Force stopping of ExecutorService threads

血红的双手。 提交于 2019-12-18 05:02:33
问题 My code: String[] torrentFiles = new File("/root/torrents/").list(); if(torrentFiles.length == 0 || torrentFiles == null) { System.exit(0); } ex = Executors.newFixedThreadPool(3); for(String torrentFile : torrentFiles) { ex.submit(new DownloadTorrent("/root/torrents/" + torrentFile)); } ex.shutdown(); try { ex.awaitTermination(30, TimeUnit.MINUTES); } catch(InterruptedException ex1) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex1); } But sometimes torrent downloading

How to give name to a callable Thread? [duplicate]

你说的曾经没有我的故事 提交于 2019-12-17 22:26:34
问题 This question already has answers here : Naming threads and thread-pools of ExecutorService (17 answers) Closed 3 years ago . I am executing a Callable Object using ExecutorService thread pool. I want to give a name to this thread. To be more specific, in older version I did this - Thread thread = new Thread(runnable Task); thread.setName("My Thread Name"); I use thread name in log4j logging, this helps a lot while troubleshooting. Now I am migrating my code from Java 1.4 to Java 1.6. I have

Java executors: wait for task termination. [duplicate]

倖福魔咒の 提交于 2019-12-17 22:25:57
问题 This question already has answers here : How to wait for all threads to finish, using ExecutorService? (25 answers) Closed 2 years ago . I need to submit a number of task and then wait for them until all results are available. Each of them adds a String to a Vector (that is synchronized by default). Then I need to start a new task for each result in the Vector but I need to do this only when all the previous tasks have stopped doing their job. I want to use Java Executor, in particular I

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

有些话、适合烂在心里 提交于 2019-12-17 20:36:15
问题 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

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

眉间皱痕 提交于 2019-12-17 19:12:10
问题 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

ExecutorCompletionService? Why do need one if we have invokeAll?

别说谁变了你拦得住时间么 提交于 2019-12-17 15:22:40
问题 If we use an ExecutorCompletionService we can submit a series of tasks as Callable s and get the result interacting with the CompletionService as a queue . But there is also the invokeAll of ExecutorService that accepts a Collection of tasks and we get a list of Future to retrieve the results. As far as I can tell, there is no benefit in using one or over the other (except that we avoid a for loop using an invokeAll that we would have to submit the tasks to the CompletionService ) and

ExecutorCompletionService? Why do need one if we have invokeAll?

对着背影说爱祢 提交于 2019-12-17 15:19:54
问题 If we use an ExecutorCompletionService we can submit a series of tasks as Callable s and get the result interacting with the CompletionService as a queue . But there is also the invokeAll of ExecutorService that accepts a Collection of tasks and we get a list of Future to retrieve the results. As far as I can tell, there is no benefit in using one or over the other (except that we avoid a for loop using an invokeAll that we would have to submit the tasks to the CompletionService ) and

Java ExecutorService: awaitTermination of all recursively created tasks

◇◆丶佛笑我妖孽 提交于 2019-12-17 07:17:30
问题 I use an ExecutorService to execute a task. This task can recursively create other tasks which are submitted to the same ExecutorService and those child tasks can do that, too. I now have the problem that I want to wait until all the tasks are done (that is, all tasks are finished and they did not submit new ones) before I continue. I cannot call ExecutorService.shutdown() in the main thread because this prevents new tasks from being accepted by the ExecutorService . And Calling