Use Callable<V>
instead of Thread
(or Runnable
) so that you can get result as Future<V>
and use ExecutorService
to invoke it.
Here's an SSCCE, just copy'n'paste'n'run it:
package com.stackoverflow.q2413389;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class Test {
public static void main(String... args) throws Exception {
ExecutorService executor = Executors.newCachedThreadPool();
List<Future<String>> results = executor.invokeAll(Arrays.asList(new Task()));
for (Future<String> result : results) {
System.out.println(result.get()); // Prints "myResult" after 2 seconds.
}
executor.shutdown();
}
}
class Task implements Callable<String> {
public String call() throws Exception {
Thread.sleep(2000); // Do your running task here, this example lasts 2 seconds.
return "myResult";
}
}
Update: as per your update with the question how to kill it after a timeout, make use of ScheduledExecutorService
instead. Here's the slightly changed code:
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
List<Future<String>> results = executor.invokeAll(Arrays.asList(new Task()), 1, TimeUnit.SECONDS); // Timeout of 1 second.
for (Future<String> result : results) {
if (!result.isCancelled()) {
System.out.println(result.get()); // Won't be printed as the "actual" processing took 2 seconds.
} else {
System.out.println("Task timed out.");
}
}
executor.shutdown();