Java 8 concurrent asynchronous lambda

Intellij recommends lambda alternatives where possible

CompletableFuture
      .supplyAsync(() -> validateRequest(id, headers))
      .thenAccept(id -> processRequest(id, req))
      .exceptionally(t -> handleException(throable, req));

supplyAsync returns a new CompletableFuture that is asynchronously completed by a task running in the ForkJoinPool.commonPool()

Thread Runnable:

   Runnable r = () -> {...};
   Thread t = new Thread(r);
    t.start();

    new Thread( () -> {...} ).start();


Predicate - boolean expression:

    while (!acctStates.values().stream().allMatch(s -> {
      return s.getState().equals(AcctState.OPEN);
    }))
    {
      Thread.sleep(100);
    }


Local method call:

getSomeCollection().forEach(this::processElement);

Stream, Filter, Map, to new Collection:

Collection acctStates =
        accts.values().stream().filter(acct -> curUser.equals(acct.getOwner()))
            .map(acct -> acctStates.get(acct.getOwner())).filter(acct -> acct != null)
            .filter(acct -> acct.getState() == AcctState.OPEN).collect(Collectors.toList());

Get min from Collection:

Optional minState =
        acctStates.stream().min((s1, s2) -> s1.getAcctTxCount() - s2.getAcctTxCount());

Lambda toUnchecked:

        recs.forEach(r -> Lambda.toUnchecked(() -> {
          out.writeObject(r.getKey());
          out.writeObject(r.getValue());

        }));

Function passing:

getBuf(0, "foo", "bar", "baz", s -> s);
// s->s is equivalent to String apply(String s) {return s;}  where sn is passed to s

byte[] getBuf(int numOfRecs, String foo, String bar, String baz, Function tokenGen)
      throws IOException
  {
...
    try (...)
    {
      IntStream.range(0, numOfRecs).mapToObj(i -> id(foo, bar, i))
          .map(sn -> new FooRecord(sn, baz).setFoo(tokenGen.apply(sn)))
          .forEach(rec -> Lambda.toUnchecked(() -> {
            out.writeObject(rec.getId());
            out.writeObject(rec);
          }));
    }
    return buf.toByteArray();
  }

Comments

Popular posts from this blog

Sites, Newsletters, and Blogs

Oracle JDBC ReadTimeout QueryTimeout