Saturday, 16 May 2015

Callable and Future

//****************************************************************
//* Copyright (c) 2015. All Rights Reserved.
//****************************************************************
package com.java.thread.callable;

import java.io.IOException;
import java.util.Random;
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 CallableAndFuture {
    public static void main(final String[] args) {
        final ExecutorService executor = Executors.newCachedThreadPool();

        final Future<Integer> future = executor.submit(new Callable<Integer>() {

            @Override
            public Integer call() throws Exception {
                final Random random = new Random();
                final int duration = random.nextInt(4000);

                if (duration > 2000) {
                    throw new IOException("Sleeping for too long.");
                }

                System.out.println("Starting ...");

                try {
                    Thread.sleep(duration);
                } catch (final InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                System.out.println("Finished.");

                return duration;
            }

        });

        executor.shutdown();

        try {
            System.out.println("Result is: " + future.get());
        } catch (final InterruptedException e) {
            e.printStackTrace();
        } catch (final ExecutionException e) {
            final IOException ex = (IOException)e.getCause();

            System.out.println(ex.getMessage());
        }
    }
}

Note: We can use Void class if we don't want to return any thing from callable. In future<Void> we can use "?" also.

final ExecutorService executor = Executors.newCachedThreadPool();
final Future<Void> future = executor.submit(new Callable<Void>() {

        @Override
        public Void call() throws Exception {

            return null;

        }

    });


Kindly provide your valuable feedback in comment box.

No comments:

Post a Comment