In Futures.transform, what is the difference between using a function and an AsyncFunction

I know that the apply method of a function returns an object synchronously, and applying AsyncFunction is asynchronous and returns Future.

Can you give me an example of when you feel better.

One piece of code that I saw looked something like this:

Futures.transform(someFuture, new AsyncFunction<A, B>() { public B apply(A a) { if (a != null) { return Futures.immediateFuture(a.getData()) } else { return Futures.immediateFailedFuture(checkException(()); } }); }); 

Since the value inside AsyncFunction is returned as an immediate result, why do I need AsyncFunction? Or is this just a bad example that I came across?

+6
source share
4 answers

The selected code snippet is a bad example, because it uses AsyncFunction for something that is being evaluated synchronously. It is useless a lot.

The code will be cleaner using the standard Function :

 Futures.transform(someFuture, new Function<A, B>() { public B apply(A a) { if (a != null) { return a.getData(); } else { throw checkException(); } }); }); 

You should use AsyncFunction when the code converting A to B is asynchronous. In your example, it is possible that the code was asynchronous at first, and was later changed to use Futures.immediateFuture() / Futures.immediateFailedFuture() programmer who did not replace AsyncFunction with Function . Or maybe he just skipped the overloaded method.

+10
source

Since the value inside AsyncFunction is returned as an immediate result, why do I need AsyncFunction? Or is this just a bad example of what I came across?

Meticulous. This piece of code generates an instance of the anonymous class and passes that instance to the transform method. The transform method will use AsyncFunction - a separate thread. Future returns chains to retrieve results from AsyncFunction and returns the result of this Future . This code still includes asynchronous processing.

Use asynchronous processing whenever you want and can continue to do the work while something else is running.

+1
source

The code snippet you gave is a bad example, the one who wrote it should have used the function.

Futures.transform () is used to track some asynchronous processes. Let's call it "ap1" for "asynchronous process 1". When ap1 is completed, the function encoded by the conversion will be executed. Now let's discuss two types of functions that you can associate using Futures.transform ().

  // AsyncFunction() example: ListenableFuture<B> future2 = Futures.transform(future1, new AsyncFunction<A, B>() { public ListenableFuture<B> apply(A a) { if (a != null) { ListenableFuture<B> future3 = asyncCallToOtherService(a); return future3; } else { return Future.immediateFuture(null); } }); }); // more complex AsyncFunction() example: ListenableFuture<B> future2 = Futures.transform(future1, new AsyncFunction<A, B>() { public ListenableFuture<B> apply(A a) { if (a != null) { ListenableFuture<C> future3 = asyncCallToOtherService(a); return Futures.transform(future3, new Function<C, B>() { @Override public B apply(C c) { B b = new B(); b.setResult(c.getStatus()); return b; } }); } else { return Future.immediateFuture(null); } }); }); // Function() example: ListenableFuture<B> future2 = Futures.transform(future1, new Function<A, B>() { public B apply(A a) { if (a != null) { B b = new B(); b.setResult(a.getStatus()); return b; } else { return null; } }); }); 
  • AsyncFunction () should be used when the code inside apply () creates another asynchronous process 2 (AP2). This forms a sequence of asynchronous calls that follow each other: AP1-> AP2.
  • Function () should be used when converting the result from AP1 does not require additional asynchronous processes. Most likely, this synchronously translates the result of Object1 to another Object2.
  • AsyncFunction () is a bit heavier and leads to a separate topic, so we should use function () when we do not need to rotate AP2.
  • Multiple functions and AsyncFunctions can be combined together as needed to make a complete workflow. The example "more complex AsyncFunction () example" converts A ~> C-> B transforms, and A ~> C is asynchronous.
+1
source

Using AsyncFunction here makes sense. If you want to throw an excluded exception from the apply () method in a function, it will complain that it is not being processed. You cannot throw it from apply () so that it is overridden. Therefore, if you want to throw some kind of checked exception, AsyncFunction should be a valid solution. For those who say that this example is bad and an example of a function is given, can you try compiling it?

0
source

Source: https://habr.com/ru/post/970351/


All Articles