Skip to content

Instantly share code, notes, and snippets.

@ykrkn
Created July 14, 2016 21:36
Show Gist options
  • Save ykrkn/d3538dc8f6136c7e382580b31a2d7ebb to your computer and use it in GitHub Desktop.
Save ykrkn/d3538dc8f6136c7e382580b31a2d7ebb to your computer and use it in GitHub Desktop.
CompletableFuture
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;
import java.util.function.Supplier;
/**
* Created by sx on 26.02.16.
*/
public class Main implements Supplier<Double>, Consumer, Runnable{
private final AtomicLong cnt = new AtomicLong();
public static void main(String[] args){
System.out.println("RUN");
new Main().run0();
}
private void run0(){
CompletableFuture.supplyAsync(this)
.thenApply(x -> Math.sqrt(x))
.thenApply(x -> Math.sqrt(x))
.thenAccept(x -> System.out.print(x))
.thenRun(Main.this);
}
@Override
public Double get() {
return 256.0 ;//Math.random();
}
@Override
public void accept(Object o) {
System.out.println(o);
try {
Thread.sleep(111);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run(){
try {
Thread.sleep(7777);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment