A Better Way To Do A Thread.Join()

Java 5 concurrency libraries provide a good way create thread pool, spawn off a task and wait for the result.

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;
import java.util.concurrent.FutureTask;
 
public class FutureTaskDemo {
 
    public final static void main(String... args) {
 
        final Callable<Integer> callable = new Callable<Integer>() {
 
            @Override
            public Integer call() throws InterruptedException {
                System.out.println("starting to sleep thread '"
                        + Thread.currentThread().getName() + "'.....");
                Thread.sleep(3000);
                System.out.println("finished sleeping thread '"
                        + Thread.currentThread().getName() + "'.");
                return new Integer(6);
            }
        };
 
        final ExecutorService execute = Executors.newCachedThreadPool();
        final Future<Integer> futureTask = execute.submit(callable);
 
        System.out.println("about to get() via thread '"
                + Thread.currentThread().getName() + "'.");
        try {
            Integer result = futureTask.get();
            System.out.println("get() = '"+result+"' via thread '"
                    + Thread.currentThread().getName() + "'.");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        execute.shutdown();
    }
}

this results in the following output

about to get() via thread 'main'.
starting to sleep thread 'pool-1-thread-1'.....
finished sleeping thread 'pool-1-thread-1'.
get() = '6' via thread 'main'.
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License