0

I've inherited a huge Java J2EE application (Java 1.8, Tomcat 8). It takes 30 seconds to logon - mostly because of multiple sequential AJAX calls (from the Java server, NOT JavaScript) to various services. One in particular takes over 10 seconds to return.

I don't need that result until towards the end - so I could initialize the AJAX call at the start, do other stuff, then sync up & use the result at the end.

I've never worked with Java multi-threading, but that seems like one way to achieve that. But not wanting to wait for the result of an AJAX call seems like a common use case. Are there any tools/libraries/conventions for this?

And this class has a lot of other methods - I don't (think) I want to make the whole class "Runnable" - would I make a couple of inner classes - "waiter & notifier", or??? Sorry, just never worked with threading in Java & trying to figure out where to begin....

Thanks...

ChrisNY
  • 3,917
  • 4
  • 27
  • 31
  • The `A` of `Ajax` means Asynchronous, so why are they running sequentially? if you really really need then to be synchrounousm then have a look at https://stackoverflow.com/questions/16026942/how-do-i-chain-three-asynchronous-calls-using-jquery-promises – Scary Wombat Dec 05 '18 at 02:59
  • this is a broad question, maybe too broad for stackoverflow. if you've never worked with threading in java, i'd suggest you get started by searching for a tutorial on this topic. – jdigital Dec 05 '18 at 03:08
  • Agreed A means Asynchronous. See the first line "inherited" - just trying to fix a messy jumble I wouldn't have written. – ChrisNY Dec 05 '18 at 03:21
  • As far as too broad, yes, I don't expect a tutorial on Java multi-threading here - I am studying that. My question was more if there are any existing methods/libraries that simplify that simple use case. – ChrisNY Dec 05 '18 at 03:23
  • From [What topics can I ask about here?](https://stackoverflow.com/help/on-topic) -- _Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it._ – jdigital Dec 05 '18 at 05:09

1 Answers1

0

OK, for anyone else with this issue, after a little research the solution for my usecase was pretty straight forward.

1) I made an inner class that implemented "Runnable". So it had access to all the properties of the enclosing class. In the "run" method of the inner class, I made the AJAX call.

2) In the original method at the top, I instantiated & started the inner class thread:

Thread gml = new Thread(new GetModuleList());
gml.start();

3) Did all the other stuff that didn't depend on the return from gml...

4) When I needed it, I just did "gml.join()".

Seems to work fine, & not as complicated as I initially worried it might be. The inner class was the trick for me - took care of all the thread data sharing, as long as my main thread stays away from the properties set by the worker thread until after the join.

ChrisNY
  • 3,917
  • 4
  • 27
  • 31