Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to asynchronously call a method in Java

I've been looking at Go's goroutines lately and thought it would be nice to have something similar in Java. As far as I've searched the common way to parallelize a method call is to do something like:

final String x = "somethingelse"; new Thread(new Runnable() {            public void run() {                 x.matches("something");                  } }).start(); 

Thats not very elegant. Is there a better way of doing this? I needed such a solution in a project so I decided to implement my own wrapper class around a async method call.

I published my wrapper class in J-Go. But I don't know if it is a good solution. The usage is simple:

SampleClass obj = ... FutureResult<Integer> res = ... Go go = new Go(obj); go.callLater(res, "intReturningMethod", 10);         //10 is a Integer method parameter //... Do something else //... System.out.println("Result: "+res.get());           //Blocks until intReturningMethod returns 

or less verbose:

Go.with(obj).callLater("myRandomMethod"); //... Go away if (Go.lastResult().isReady())                //Blocks until myRandomMethod has ended     System.out.println("Method is finished!"); 

Internally I'm using a class that implements Runnable and do some Reflection work to get the correct method object and invoking it.

I want some opinion about my tiny library and on the subject of making async method calls like this in Java. Is it safe? Is there already a simplier way?

like image 721
Felipe Hummel Avatar asked Dec 03 '09 20:12

Felipe Hummel


People also ask

How do you call a method asynchronously?

The simplest way to execute a method asynchronously is to start executing the method by calling the delegate's BeginInvoke method, do some work on the main thread, and then call the delegate's EndInvoke method. EndInvoke might block the calling thread because it does not return until the asynchronous call completes.

How do you make a method asynchronous in Java?

Asynchronous Callback So in the context of Java, we have to Create a new thread and invoke the callback method inside that thread. The callback function may be invoked from a thread but is not a requirement. A Callback may also start a new thread, thus making themselves asynchronous.


2 Answers

I just discovered that there is a cleaner way to do your

new Thread(new Runnable() {     public void run() {         //Do whatever     } }).start(); 

(At least in Java 8), you can use a lambda expression to shorten it to:

new Thread(() -> {     //Do whatever }).start(); 

As simple as making a function in JS!

like image 154
AegisHexad Avatar answered Sep 19 '22 15:09

AegisHexad


Java 8 introduced CompletableFuture available in package java.util.concurrent.CompletableFuture, can be used to make a asynch call :

CompletableFuture.runAsync(() -> {     // method call or code to be asynch. }); 
like image 45
Rahul Chauhan Avatar answered Sep 23 '22 15:09

Rahul Chauhan