Avatar of 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 ?


Avatar of undefined
Last Comment

8/22/2022 - Mon

by the way I already saw the following link so no need to post it again :

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
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

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
        } catch (Exception e) {

public class AsyncDriver {
    private ExecutorService executorService;

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

    public void callAsync(AsyncMethod method) {

Open in new window


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
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.