How can Thread return a value after shutdown?

Let's say we have such a simple example:

public Example extends Thread{ String temp; public Example(){ } @Override public void run(){ . . . . temp = "a_value"; } public static void main(String[] args) { Example th = new Example(); th.start(); } } 

How will Thread return String temp after completion?

+41
java multithreading
Jun 29 '10 at 13:36
source share
4 answers

Use (relatively) the new Callable<T> instead of Runnable (available in versions 1.5 and later):

Here is an example (simple):

 import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class Main { public static void main(final String[] argv) { final ExecutorService service; final Future<String> task; service = Executors.newFixedThreadPool(1); task = service.submit(new Foo()); try { final String str; // waits the 10 seconds for the Callable.call to finish. str = task.get(); // this raises ExecutionException if thread dies System.out.println(str); } catch(final InterruptedException ex) { ex.printStackTrace(); } catch(final ExecutionException ex) { ex.printStackTrace(); } service.shutdownNow(); } } class Foo implements Callable<String> { public String call() { try { // sleep for 10 seconds Thread.sleep(10 * 1000); } catch(final InterruptedException ex) { ex.printStackTrace(); } return ("Hello, World!"); } } 
+62
Jun 29 '10 at 13:43 on
source share

Check out the Future javadoc interface. It has a usage example showing how to do this.

+11
Jun 29 '10 at 13:41
source share

You can achieve this with the Observer pattern. at the end of the stream, notifies all listeners of the completion of work, and they can get the value (via getter). Or he can even send the calculated value.

Or you can use a task, see FutureTask , runnable (indeed, as Callable below), which returns a result and may throw exceptions.

+7
Jun 29 '10 at 13:39
source share

If you do not want to change the decision to use Callable objects, you can also use queues and return the result from the threads in this way.
I rewrote your example as follows:

 import java.util.PriorityQueue; import java.util.Queue; public class GetResultFromThread { public static void main(String[] args) throws Exception { Queue<String> queue = new PriorityQueue<String>(); int expectedResults = 2; for (int i = 0; i < expectedResults; i++) { new Example(queue).start(); } int receivedResults = 0; while (receivedResults < expectedResults) { if (!queue.isEmpty()) { System.out.println(queue.poll()); receivedResults++; } Thread.sleep(1000); } } } class Example extends Thread { private final Queue<String> results; public Example(Queue<String> results) { this.results = results; } @Override public void run() { results.add("result from thread"); } } 

Please note that you should think about synchronization and concurrency!

0
Jun 29 '10 at 16:17
source share



All Articles