NIO and SQS(1)CompletableFuture Basic

NIO and SQS(1)CompletableFuture Basic

Future in JDK5
ExecutorService es = Executors.newFixedThreadPool(10);
Future<Integer> f = es.submit( () -> {
    //cpu usage
    return 100;
});
 
while(!f.isDone()){
    …snip...
}

Or

f.get();

f.get() will block the thread and wait for the result, while will loop the CPU.

Java 8 CompletableFuture
That is a class implement interface of Future and CompletionStage
public class CompletableFuture<T> implements Future<T>, CompletionStage<T>

It seems it is thread pool to do the things.
package com.j2c.jobssolrconsumer;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class BasicMain {

public static CompletableFuture<Integer> compute() {
    final CompletableFuture<Integer> future = new CompletableFuture<>();
    return future;
}

public static void main(String[] args) throws Exception {
    final CompletableFuture<Integer> f = compute();

    class Client extends Thread {
        CompletableFuture<Integer> f;

        Client(String threadName, CompletableFuture<Integer> f) {
            super(threadName);
            this.f = f;
        }

        public void run() {
            try {
                System.out.println(this.getName() + ": " + f.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
    }
    new Client("Client1", f).start();
    new Client("Client2", f).start();
    System.out.println("waiting");
    f.complete(100);
    // f.completeExceptionally(new Exception());
    System.in.read();
    }
}

Async will use Executor —> ForkJoinPool.commonPool() That is the thread pool.

public CompletableFuture<T> whenComplete()
public CompletableFuture<T> whenCompleteAsync()
public CompletableFuture<T> exceptionally()

package com.j2c.jobssolrconsumer.completablefuture;

import java.util.Random;
import java.util.concurrent.CompletableFuture;

public class BasicMain2 {
    private static Random rand = new Random();
    private static long t = System.currentTimeMillis();

    static int getMoreData() {
        System.out.println("begin to start compute");
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("end to start compute. passed " + (System.currentTimeMillis() - t) / 1000 + " seconds");
        return rand.nextInt(1000);
    }

    public static void main(String[] args) throws Exception {
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(BasicMain2::getMoreData);
        future.whenComplete((v, e) -> {
            System.out.println(v);
            System.out.println(e);
        });
        System.out.println("system run here.");
        // System.out.println(f.get());
        System.in.read();
    }
}

public <U> CompletableFuture<U>  handle()
public <U> CompletableFuture<U> handleAsync()

Transform
combine some steps together

public <U> ComletableFuture<U> thenApply()
public <U> ComletableFuture<U> thenApplyAsync()

The first result will be pass to the function in the thenApplySync

Void Action
No result need to be returned

public ComletableFuture<Void> thenAccept()
public ComletableFuture<Void> thenAcceptAsync()

thenAccpetBoth will do the same thing for 2 CompletionStage

public <U> CompletableFuture<Void> thenAcceptBoth()
public <U> CompletableFuture<Void> thenAcceptBothAsync()
public     CompletableFuture<Void> runAfterBoth()

Compose Stage
It will compose the previous CompletableFuture and the current CompletableFuture

public <U> CompletableFuture<U> thenCompose()
public <U> CompletableFuture<U> thenComposeAsync()

Combine Action
public <U, V> CompletableFuture<V> thenCombine()
public <U, V> CompletableFuture<V> thenCombineAsync()

Either Action

Helper Methods allOf and anyOf


References:
http://colobu.com/2016/02/29/Java-CompletableFuture/
http://www.jianshu.com/p/6f3ee90ab7d3
https://www.ibm.com/developerworks/cn/java/j-jvmc2/index.html





猜你喜欢

转载自sillycat.iteye.com/blog/2395351
今日推荐