Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
FuturesA.java Simple example of using Futures.
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class FuturesA {
public static void run() throws Exception {
ExecutorService executor = new ThreadPoolExecutor(4, 4, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>());
Future<String> f1 = executor.submit(new CallToRemoteServiceA());
Future<String> f2 = executor.submit(new CallToRemoteServiceB());
System.out.println(f1.get() + " - " + f2.get());
}
public static void main(String args[]) {
try {
run();
} catch (Exception e) {
e.printStackTrace();
}
}
private static final class CallToRemoteServiceA implements Callable<String> {
@Override
public String call() throws Exception {
// simulate fetching data from remote service
Thread.sleep(100);
return "responseA";
}
}
private static final class CallToRemoteServiceB implements Callable<String> {
@Override
public String call() throws Exception {
// simulate fetching data from remote service
Thread.sleep(40);
return "responseB";
}
}
}
@MagIciaNGTAO

This comment has been minimized.

Show comment Hide comment
@MagIciaNGTAO

MagIciaNGTAO Apr 23, 2014

Will you consider add those after the io?

    executor.shutdown();
    executor.awaitTermination(100, TimeUnit.MILLISECONDS);

Will you consider add those after the io?

    executor.shutdown();
    executor.awaitTermination(100, TimeUnit.MILLISECONDS);
@ghost

This comment has been minimized.

Show comment Hide comment
@ghost

ghost Oct 19, 2014

The expected output is that System.out.println(f1.get() + " - " + f2.get());
will take 140 ms to return?

ghost commented Oct 19, 2014

The expected output is that System.out.println(f1.get() + " - " + f2.get());
will take 140 ms to return?

@manojkumar16

This comment has been minimized.

Show comment Hide comment
@manojkumar16

manojkumar16 Apr 30, 2015

No, both future will start in parallel. It will take less than (100+40) ms.

No, both future will start in parallel. It will take less than (100+40) ms.

@mabh

This comment has been minimized.

Show comment Hide comment
@mabh

mabh Sep 28, 2015

expression in sysout will take approximately 100ms to get evaluated

mabh commented Sep 28, 2015

expression in sysout will take approximately 100ms to get evaluated

@limxtop1989

This comment has been minimized.

Show comment Hide comment
@limxtop1989

limxtop1989 Apr 13, 2017

It will take 140ms approximately if there is single core, otherwise it will do 100ms

It will take 140ms approximately if there is single core, otherwise it will do 100ms

@amitp85

This comment has been minimized.

Show comment Hide comment
@amitp85

amitp85 Aug 2, 2017

@limxtop1989 It will not take 140ms on single core because both threads can sleep in parallel, it will not wait for one thread's sleep to finish before executing another thread.

amitp85 commented Aug 2, 2017

@limxtop1989 It will not take 140ms on single core because both threads can sleep in parallel, it will not wait for one thread's sleep to finish before executing another thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment