-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
What is the value of this? #54
Comments
The point of the project is to make it look very easy to write non-blocking code. The project includes a class transformer that rewrites the bytecode of your methods so that they don't actually block any threads. When the execution of the method hits a call to |
First of all, thank you for sharing here is my code: `import com.ea.async.Async; import java.io.BufferedReader; import static com.ea.async.Async.await; public class DemoCases
}` |
The reason might be that the class transformer can't rewrite the class that's already executing. I'd suggest using the Java agent or the Maven plugin instead of relying on |
I have used agent, but the result is the same I don't know where the problem is, You used it earlier, can you show your code? |
I have found this library very useful, since it enables much simpler non blocking Rest API code than using a callback based model. Many other languages have this syntax, but EA Async is the only tool that enables it for Java. It is quite sad that this is not part of the language itself:
As an example, try to write the code in this class using a callback based syntax. My blog has an advanced Java Spring Boot API that uses EA Async, and equivalent APIs in C# / NodeJS that implement the same requirements. For further Java coding details, see this blog post of mine on the Java non blocking coding model. |
@itboy2009 can you try this? public CompletableFuture<String> blockSomeThingFuture()
{
System.out.println("blockSomeThingFuture(): enter");
CompletableFuture<String> ret = CompletableFuture.supplyAsync(() -> {
for(int i=0;i<5;i++) {
try
{
System.out.println("blockSomeThingFuture(): before blockIO() call");
int contentLength = blockIO();
System.out.println(String.format("contentLength: %s, threadId: %s", contentLength, Thread.currentThread().getId()));
}
catch (Exception e)
{
e.printStackTrace();
}
}
return "done";
}, workThreadPool);
System.out.println("blockSomeThingFuture(): exit");
return ret;
} And see if "blockSomeThingFuture(): exit" is output before "blockSomeThingFuture(): before blockIO() call"? If it isn't, then |
If it helps anyone I've put together a very simple Non Blocking Java Async Await API to demonstrate usage: Code should be simple when using an async await coding model:
|
here is the output: blockSomeThingFuture(): enter but, I don’t use ea-async, I can get the same output. I mean, during the IO blocking period, ea-async did not release the thread to do other things |
Use myAwait() instead of await(), you can get the same result Is this the value of ea-async? I'm confused |
Are you using the maven or gradle plugins to transform the classes before attempting to decompile them? I just tried the following simple project: import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.CompletableFuture;
import com.ea.async.Async;
public class Program {
public static void main(String[] args) {
Async.init();
Test test = new Test();
CompletableFuture<String> future = test.blockSomeThingFuture();
Test.log("awaiting future");
String result = Async.await(future);
Test.log("complete: " + result);
}
}
class Test {
private static final Timer timer = new Timer();
private static final Random random = new Random();
public CompletableFuture<String> blockSomeThingFuture()
{
log("blockSomeThingFuture(): enter");
try {
Async.await(simulateIo());
return CompletableFuture.completedFuture("Hello!");
} finally {
log("blockSomeThingFuture(): exit");
}
}
private CompletableFuture<Void> simulateIo() {
CompletableFuture<Void> future = new CompletableFuture<>();
long delay = random.nextInt(1000) + 1000;
timer.schedule(new TimerTask() {
@Override
public void run() {
future.complete(null);
}
}, delay);
return future;
}
public static void log(String message) {
Thread currentThread = Thread.currentThread();
System.out.printf("[%d:%s] %s%n", currentThread.getId(), currentThread.getName(), message);
}
} The output was:
You can see from the output that |
Thanks @HaloFour - I've used EA Async quite a bit in the past for sample purposes - the goals behind it are good - and when I've looked at decompiled code it has always looked correct. A busy week at work this week so maybe I'm just confusing myself. Will look at your code at the weekend. Enjoying the discussion with you guys though ... |
I haven't used ea-async in a while myself. My more recent projects are all based on Spring WebFlux and I never found a good synergy between the two nor do I feel like trying to adapt this project to work with Reactor. One project you might want to keep your eyes on is Project Loom which looks to bring coroutine primitives and green threads to the Java runtime. Using them enables writing your own helper methods that behave like coroutines with parked green threads as the async state machine. I've emulated |
@HaloFour When debugging, you can see all variables in the monitor window just like when debugging normal code. |
await(blockSomeThingFuture());
just only equals to
this.blockSomeThingFuture().toCompletableFuture().join(); // block!!!
Why is it so complicated?
Or I didn’t understand, I look forward to your sharing
The text was updated successfully, but these errors were encountered: