Create FutureTask from Callable interface - Java Thread

Java examples for Thread:FutureTask

Description

Create FutureTask from Callable interface

Demo Code



import java.util.concurrent.*;


class CallWorker implements Callable<String> {

    @Override//  w ww.  j  av  a  2 s .co  m
    public String call() throws Exception {
        return Thread.currentThread().getName();
    }
}

public class TestFutureTask {

    public static void main(String[] args) {
        ExecutorService service = Executors.newCachedThreadPool();
        FutureTask<String> task = new FutureTask<String>(new CallWorker());
        service.execute(task);
        try {
            String result = task.get();
            System.out.println(result);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        service.shutdown();
    }
}

Related Tutorials