*
The moose likes Servlets and the fly likes Which of these setups would run faster? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Which of these setups would run faster? " Watch "Which of these setups would run faster? " New topic
Author

Which of these setups would run faster?

Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
Hi, I am wondering about the theory behind how the Java language works and which one of these setups would be faster (if any at all).

So here is the scenario.
- JSP page with a form input
- POST to "FormServlet.java"
- Within "FormServlet.java", there is a call to another java file "DoTheThing.java"
- The time "DoTheThing.java" takes to run is 10 minutes (made up number)

Now we don't want the user waiting around for 10 minutes until we show them a "confirmation.jsp" page, and we could use AJAX to POST without leaving the page (but in this scenario, we don't want to for some unknown reason).

So I was wondering if there would be any difference between these two options.

Option 1

Within "FormServlet.java" we could have the following statement

In this scenario, the class "DoTheThing.java" does not return any data.

Option 2

Within "FormServlet.java" we could have the following statement

In this scenario, the class "DoTheThing.java" would return a true/false once it has completed its 10 minute task.

So the question is, by just sending data to a Java class which is 'void', does the servlet "FormServlet.java" continue on with the next line of code once the data has been passed across - or - does it wait around until the process has finished in "DoTheThing.java"? If the original servlet does not have to wait until "DoTheThing.java" to complete, then this would mean that the user could be shown a "confirm.jsp" page much faster and continue about their day.

Obviously when waiting for a returned value from the "DoTheThing.java" class, the original servlet would have to wait around for an answer since this would most likely be used further down in the servlet.

Since I don't have any classes that take 10 minutes to run, I cannot test this myself.

Interesting to find out as this will influence how I develop future code.

Thanks
Michael

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60781
    
  65

No difference whatsoever. Servlets are Java classes like any other and exhibit the same behavior as any other class. If a method takes 10 minutes to execute, it won't return to the calling code until it's done, regardless of whether the return type is void or not.

Long running processes have been a subject of conversation before in this forum. Generally, consensus is to fire off a separate thread for the long process and return to the user. Give the user a way to come back at a later time and check progress, and fetch results.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
Ok that's good to know.

Is there a specific package/method which allows easy implementation to make a class run in a separate thread?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60781
    
  65

Search the forum for previous discussions.

Ben Souther also provided an example.
subodh gupta
Ranch Hand

Joined: Jul 23, 2007
Posts: 203
This can work provided you need to do some more coding for it as follows:
1. create a runnable in go method write your logic in run method
2. start the thread and return
3. Check in the jsp via some flag if thread finished
4. use getter to get the result
My only point is you can do this. However this solution is not good.


http://subodh-gupta.blogspot.com
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Michael Cropper wrote:Since I don't have any classes that take 10 minutes to run...


But it's easy to write a method which takes 10 minutes to run:
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60781
    
  65

subodh gupta wrote:3. Check in the jsp via some flag if thread finished

Don't bother. No one is going to stick around for 10 minutes waiting for something to complete. They're gonna go watch YouTube videos or something.

As I said, have way for them to come back at a later time and view the status of any long-running background operations and to fetch the results for those that are complete.
Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
Thanks all, I will do some more research on the different points mentioned.

And thanks Paul, didn't realise it was that easy!!

Michael
subodh gupta
Ranch Hand

Joined: Jul 23, 2007
Posts: 203
Yeah Paul I agree it was just to meet the usecas and i mentioned in post

"My only point is you can do this. However this solution is not good. "
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Which of these setups would run faster?
 
Similar Threads
Struggling to run a script at regular intervals - any ideas?
wait and notify
asynchronous coomunication using java & servlets
IllegalMonitorStateException
asynchronous communication between java and servlets