executorservice

Is thread starvation deadlock happening here in the code?

不羁岁月 提交于 2019-11-30 15:51:13
//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 Callable<String> { public String call() throws Exception { Future<String> header, footer; header = exec

How to stop a ScheduledExecutorService?

天大地大妈咪最大 提交于 2019-11-30 12:45:31
The program finishes after nine prints: class BeeperControl { private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); public void beep() { final Runnable beeper = new Runnable() { public void run() { System.out.println("beep"); } }; final ScheduledFuture<?> beeperHandle = scheduler.scheduleAtFixedRate( beeper, 1, 1, SECONDS); scheduler.schedule(new Runnable() { public void run() { beeperHandle.cancel(true); } }, 1 * 9, SECONDS); } public static void main(String[] args) { BeeperControl bc = new BeeperControl(); bc.beep(); } } How to stop a process (i.e. java

Java set a callback from ExecutorService

故事扮演 提交于 2019-11-30 11:45:37
I have a fixedThreadPool that I am using to run a bunch of worker threads to achieve parallel execution of a task with many components. When all threads have finished, I retrieve their results (which are quite large) using a method (getResult) and write them to a file. Ultimately, to save memory and be able to see intermediate results, I'd like each thread to write its result to the file as soon as it finishes execution and then free its memory. Ordinarily, I'd add code to that effect to the end of the run() method. However, certain other objects in this class also calls these threads, but DO

Does an ExecutorService get garbage collected when out of scope?

不想你离开。 提交于 2019-11-30 11:06:47
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.newSingleThreadExecutor(runnable -> { Thread thread = new Thread(runnable, "documentId=" + name); thread.setDaemon(true);

Does a Future timeout kill the Thread execution

蓝咒 提交于 2019-11-30 10:28:19
问题 When using an ExecutorService and Future objects (when submitting Runnable tasks), if I specify a timeout value to the future's get function, does the underlying thread get killed when a TimeoutException is thrown? 回答1: It does not. Why would it? Unless you tell it to. There is a very valid concern here in case of a Callable for example. If you waited for the result for say 20 seconds and you did not get it, then you are not interested in the result anymore. At that time you should cancel the

Thread.join() equivalent in executor

给你一囗甜甜゛ 提交于 2019-11-30 07:05:16
问题 I have a newbie question. I have this code: public class Main { public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub IntHolder aHolder=new IntHolder(); aHolder.Number=0; IncrementorThread A= new IncrementorThread(1, aHolder); IncrementorThread B= new IncrementorThread(2, aHolder); IncrementorThread C= new IncrementorThread(3, aHolder); A.start(); B.start(); C.start(); A.join(); B.join(); C.join(); System.out.println("All threads completed..."

converting to ScheduledThreadPoolExecutor

不想你离开。 提交于 2019-11-30 05:26:27
I am still a beginner at Java so I have not learned much about threads and concurrency. However, I would like to be able to use the ScheduledThreadPoolExecutor as a timer because of the problems I am having with java.util.Timer and TimerTask. I am extremely interested in the creation of threads and know that I will be learning about them in a few weeks. However, if possible could someone give me a basic example on how to convert my current mini test program using util.timer to using a ScheduledThreadPoolExecutor? I would like to complete this example ASAP so I don't have much time to learn

Spring Async issue when upgrading from 4.2.0.RC3 to 4.2.0.RELEASE

折月煮酒 提交于 2019-11-30 03:23:15
I've a web application using the spring(4.2.x) artifacts spring-webmvc, spring-messaging, spring-websocket I've the below @Enable* annotations in my spring config java class @EnableWebMvc @EnableWebSocketMessageBroker @EnableAsync @EnableMBeanExport WebSocket is used for broadcasting messages to browser clients. And there are few async methods annotated with @Async The application was working fine with spring version 4.2.0.RC3. But when I changed it to the GA release 4.2.0.RELEASE, I get the below exception on startup. If I remove @EnableAsync it works fine, but I need the async functionality.

Does a Future timeout kill the Thread execution

删除回忆录丶 提交于 2019-11-29 21:14:48
When using an ExecutorService and Future objects (when submitting Runnable tasks), if I specify a timeout value to the future's get function, does the underlying thread get killed when a TimeoutException is thrown? It does not. Why would it? Unless you tell it to. There is a very valid concern here in case of a Callable for example. If you waited for the result for say 20 seconds and you did not get it, then you are not interested in the result anymore. At that time you should cancel the task at all. Something like this: Future<?> future = service.submit(new MyCallable()); try { future.get(100

java code execution yields to different results in debug without breakpoints and normal run. Is ExecutorService broken?

最后都变了- 提交于 2019-11-29 20:28:08
TL:DR ExecutorService executorService = Executors.newFixedThreadPool(8); in debug runs concurrent, but in normal runtime it starts concurrent, but later runs in single thread. I have some code where I start 4 different tasks in ExecutorService . Two of those tasks should finish almost instantly, the other two should run for a while. Those tasks return execution time in seconds in Future<Double> . This code is responsible for task execution and measurement: public Future<Double> measure(int[] arr, ProcessIntArray processIntArray, ExecutorService es) { Callable<Double> task = () -> { long start