Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6241587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:47:39+00:00 2026-05-24T11:47:39+00:00

I read answers from similar Q&A How do you create an asynchronous HTTP request

  • 0

I read answers from similar Q&A

How do you create an asynchronous HTTP request in JAVA? |
Asynchronous programming design pattern |
AsyncTask Android – Design Pattern and Return Values

I see a lot of solutions , but none really satifies me.

Listener way

Once the results are caught, the processing is implemented in onResult method.

public interface GeolocationListener {
public void onResult(Address[] addresses);
public void onError(Exception e);
}

This solution doesn’t quite satify me , because I want to handle the results in the main method. I hate this interface because when the response is returned, it is processed in onResult resulting in chains of processing and no way to go back to the “main” method.

The servlet way

public class SignGuestbookServlet extends HttpServlet {

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
        // ...
        resp.sendRedirect("/guestbook.jsp");
    }
}

There is no exposed Java code calling the servlet. All the configuration is done in the web.xml

The way I want

Wait for the response like this

Response a = getResponse();
// wait until the response is received, do not go further
// process
Response b = getResponse();
// wait until the response is received, do not go further
process(a,b);

Is there a design pattern to handle the async request and wait for the response like above ? Other way than the listener.
Please no library or framework.

EDIT
Thanks so far the responses. I didn’t give you the full picture so I exposed the Geolocation class
I started the implementation . I don’t know how to implement the method . Can someone shows “how to” ? He (or she) must also implement the listener to retrieve the results

private Address getFullAddress (String text, AddressListener listener, ... ){

    // new Geolocation(text, listener, options).start() 
    // implements Geolocation.GeolocationListener   
    // how to return the Address from the onResult ?
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:47:40+00:00Added an answer on May 24, 2026 at 11:47 am

    Asynchronous code can always be made synchronous. The simplest/crudest way is to make the async call, then enter a while loop that just sleeps the current thread until the value comes back.

    Edit: Code that turns an asynchronous callback into synchronous code–again, a crude implementation:

    import java.util.concurrent.*;
    
    public class MakeAsynchronousCodeSynchronous {
        public static void main(String[] args) throws Exception {
            final Listener listener = new Listener();
            Runnable delayedTask = new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        throw new IllegalStateException("Shouldn't be interrupted", e);
                    }
                    listener.onResult(123);
    
                }
            };
            System.out.println(System.currentTimeMillis() + ": Starting task");
            Executors.newSingleThreadExecutor().submit(delayedTask);
            System.out.println(System.currentTimeMillis() + ": Waiting for task to finish");
            while (!listener.isDone()) {
                Thread.sleep(100);
            }
            System.out.println(System.currentTimeMillis() + ": Task finished; result=" + listener.getResult());
        }
    
        private static class Listener {
            private Integer result;
            private boolean done;
    
            public void onResult(Integer result) {
                this.result = result;
                this.done = true;
            }
    
            public boolean isDone() {
                return done;
            }
    
            public Integer getResult() {
                return result;
            }
        }
    }
    

    You could also use a CountDownLatch as recommended by hakon’s answer. It will do basically the same thing. I would also suggest you get familiar with the java.util.concurrent package for a better way to manage threads. Finally, just because you can do this doesn’t make it a good idea. If you’re working with a framework that’s based on asynchronous callbacks, you’re probably much better off learning how to use the framework effectively than trying to subvert it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen & read over 10 similar questions but am still unable to
I have read similar questions and their answers, however, it seems none deal with
If I have URL something like below: http://test.com?x=1&x=2&x=3&x=4&x=5&x=6&x=7 Then how can I read all
I've read many answers to similar questions, but still didn't get to the answer
I read from some books that Phusion Passenger is the answer to easy Ruby
Well is there? From everything I've read, it seems like the answer is no,but
I have read the answers for What's the best way to check if a
I've read all the answers on to this questions and none of the solutions
I've read a few answers on here that condemn the use of svn:externals. I
I read some of the answers on here re: testing views and controllers, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.