Skip to content

Instantly share code, notes, and snippets.

View PatrickGopher's full-sized avatar

PatrickGopher

View GitHub Profile
@Override
public void getUserInfo(UserInfoRequest request, StreamObserver<UserInfo> response) {
final CompletableFuture<String> Result =
provider.getUserInfo(request.getUserId()); // some other grpc request invoke
final CompletableFuture<UserInfo> responseFuture =
trackResult.thenApply(result -> UserInfo.newBuilder().setUserInfo(result).build());
responseFuture.whenComplete(
(result, throwable) -> {
if (result != null) {
response.onNext(result);
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier)
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier,Executor executor)
public static CompletableFuture<Void> runAsync(Runnable runnable)
public static CompletableFuture<Void> runAsync(Runnable runnable,Executor executor)
final var a = 10;
CompletableFuture<Void> future = CompletableFuture
.supplyAsync(() -> a * (a + 1))
.exceptionally(ex -> {
System.out.println("err: " + ex.toString());
return 0;
})
.thenApply(i -> Integer.toString(i))
.thenApply(str -> "\"" + str + "\"")
.thenAccept(System.out::println);
@PatrickGopher
PatrickGopher / multithread
Created July 17, 2020 05:48
multithread java
ExecutorService executor = Executors.newFixedThreadPool(5);
List<Future<String>> list = new ArrayList<>();
for(int i=0; i< 100; i++){
Future<String> future = executor.submit(() -> "222");
list.add(future);
}
for(Future<String> fut : list){
try {
System.out.println(new Date()+ "::"+fut.get());
} catch (InterruptedException | ExecutionException e) {
@PatrickGopher
PatrickGopher / multithread
Created July 17, 2020 05:48
multithread java
ExecutorService executor = Executors.newFixedThreadPool(5);
List<Future<String>> list = new ArrayList<>();
for(int i=0; i< 100; i++){
Future<String> future = executor.submit(() -> "222");
list.add(future);
}
for(Future<String> fut : list){
try {
System.out.println(new Date()+ "::"+fut.get());
} catch (InterruptedException | ExecutionException e) {
public static void main(String[] args) {
CompletableFuture<Void> subTask = null;
try (SubmissionPublisher<Long> pub = new SubmissionPublisher<>()) {
// Create a Subscriber
SimpleSubscriber sub = new SimpleSubscriber("S1", 10);
// Create a processor
FilterProcessor<Long> filter = new FilterProcessor<>(n -> n % 2 == 1);
// Subscribe the filter to the publisher and a subscriber to the filter
pub.subscribe(filter); // publisher add processer
filter.subscribe(sub); // processer add subscriber
@PatrickGopher
PatrickGopher / java async Subscriber
Created July 17, 2020 04:10
java async Subscriber
public class NormalSubscriber implements Flow.Subscriber<Long> {
private Flow.Subscription subscription;
private String name = "NormalSubscriber";
public NormalSubscriber(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
@PatrickGopher
PatrickGopher / java async FilterProcesser
Created July 17, 2020 04:05
java async FilterProcesser
public class FilterProcessor<T> extends SubmissionPublisher<T> implements Processor<T,T>{
private Predicate<? super T> filter;
public FilterProcessor(Predicate<? super T> filter) {
this.filter = filter;
}
@Override
public void onSubscribe(Flow.Subscription subscription) {
subscription.request(5);// set up your queue length
}
@Override
@PatrickGopher
PatrickGopher / go context WithDeadline demo
Created July 3, 2020 04:02
go context WithDeadline demo
func delay(ctx context.Context, t int) {
go func() {
for {
time.Sleep(2 * time.Second)
fmt.Println("running ... ")
}
}()
select {
case <-ctx.Done():
@PatrickGopher
PatrickGopher / go context demo
Last active July 3, 2020 03:35
go context demo
func task(ctx context.Context, cnt int) {
for {
select {
case v := <-ctx.Done():
fmt.Printf("counter:%v,receive: %v, finish\n", cnt, v)
return
default:
fmt.Printf("counter:%v,finish\n", cnt)
time.Sleep(1 * time.Second)
}