Avatar of bachra04
bachra04
Flag for Canada asked on

Help needed to create asynchronous java method

Hi guys,

I need to create an asynchronous method in java. I thought about using two threads and a queue similar to C++ so I can push the method to the queue in one thread. And another thread reads the method executes it and then call the handler method. Is that possible in java ? any sample code will be appreciated ?

Thanks
Java

Avatar of undefined
Last Comment
gordon_vt02

8/22/2022 - Mon
bachra04

ASKER
by the way I already saw the following link so no need to post it again :
http://cephas.net/blog/2004/02/02/asynchronous-method-invocation-in-java/

Thanks
SOLUTION
Gurvinder Pal Singh

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
gordon_vt02

Do you need to handle multiple potential calls to this method concurrently (or in some order)?  Just wondering why you're looking to create a queue with callbacks.  If that's the case, your best bet might be to implement the async method as a Runnable object and define an interface for the callback method.  Then, provide an implementation of that interface to the Runnable and submit it to an ExecutorService (java.util.concurrent) to manage the threads for you.  This approach will run your callback method on the same thread that ran the method, but I don't know if that is a requirement for you or not.


public interface AsyncCallback {
    void asyncComplete(Object result);
    // optional
    void asyncError(Exception e);
}

public class AsyncMethod implements Runnable {
    private AsyncCallback callback;

    public AsyncMethod(AsyncCallback callback) {
        this.callback = callback;
    }

    public void run() {
        try {
            // do something
            callback.asyncComplete(...);
        } catch (Exception e) {
            callback.asyncError(e);
        }
    }
}

public class AsyncDriver {
    private ExecutorService executorService;

    public AsyncDriver() {
        executorService = Executors.XXX(); // use appropriate executor
    }

    public void callAsync(AsyncMethod method) {
        executorService.submit(method);
    }
}

Open in new window

bachra04

ASKER
Hi Gordon,

I don't have a requirement to execute the callback method in the same thread as the main method. but I do have a requirement of very high level of calls to the the asynchMethod ( Thousands per second), that's why I thought about having a queue.
But I would assume that the executorService is using an internal queue to do that ?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.