Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java non blocking notifier

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

public class GetTaskNotificationWithoutBlocking {

  public static void main(String... argv) throws Exception {
    ExampleService svc = new ExampleService();
    GetTaskNotificationWithoutBlocking listener = new GetTaskNotificationWithoutBlocking();
    CompletableFuture<String> f = CompletableFuture.supplyAsync(svc::work);
    f.thenAccept(listener::notify);
    System.out.println("Exiting main()");
  }

  void notify(String msg) {
    System.out.println("Received message: " + msg);
  }

}

class ExampleService {

  String work() {
    sleep(7000, TimeUnit.MILLISECONDS); /* Pretend to be busy... */
    char[] str = new char[5];
    ThreadLocalRandom current = ThreadLocalRandom.current();
    for (int idx = 0; idx < str.length; ++idx)
      str[idx] = (char) ('A' + current.nextInt(26));
    String msg = new String(str);
    System.out.println("Generated message: " + msg);
    return msg;
  }

  public static void sleep(long average, TimeUnit unit) {
    String name = Thread.currentThread().getName();
    long timeout = Math.min(exponential(average), Math.multiplyExact(10, average));
    System.out.printf("%s sleeping %d %s...%n", name, timeout, unit);
    try {
      unit.sleep(timeout);
      System.out.println(name + " awoke.");
    } catch (InterruptedException abort) {
      Thread.currentThread().interrupt();
      System.out.println(name + " interrupted.");
    }
  }

  public static long exponential(long avg) {
    return (long) (avg * -Math.log(1 - ThreadLocalRandom.current().nextDouble()));
  }

}
Comment

PREVIOUS NEXT
Code Example
Java :: java loop aray 
Java :: how to write a java program for printing child or adult in java 
Java :: java singly linked list example 2 res 
Java :: android studio null 
Java :: fail if condition not met in java if condition 
Java :: Bukkit dev paid account 
Java :: findbyname in jpa 
Java :: String length equality 
Java :: array erstellen java 
Java :: maximum occuring element in java 
Java :: war file in java 
Java :: Java @SafeVarargs annotation 
Java :: setCookie("showPopupBannerBeta", 1, 1 
Java :: kubernetes java client create namespace 
Java :: How to disable special characters on keyboard in android 
Java :: differance entre appel implcite et explicite en java 
Java :: system out java quick 
Java :: convert kotlin to java online 
Java :: java optional parameters 
Java :: java arrays method 
Java :: multiple return values in function java 
Java :: java resto 
Java :: java union type 
Java :: java coding standards for classes 
Java :: sololearn bowling game 
Java :: jgit clone in memory 
Sql :: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning. 
Sql :: postgresql reset sequence 
Sql :: update sql server from another table 
Sql :: mysql alter table add index 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =