aspose file tools*
The moose likes Threads and Synchronization and the fly likes Review : MultiThreading Solution Implementation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Review : MultiThreading Solution Implementation" Watch "Review : MultiThreading Solution Implementation" New topic
Forums: Threads and Synchronization Performance
Author

Review : MultiThreading Solution Implementation

Tarun Trehan
Ranch Hand

Joined: Mar 16, 2012
Posts: 49

Hi All,

I have been working with multithreading recently.
My project has a struts-spring-sql implementation.
I had a situation in my project where there were 4 independent stored procedures being called from a screen. These stored procedures fetch details from the database and show it on screen.
These procedures were called sequentially and response times were high.

I implemented multi threading here by the following:
1. Make two WorkerThread classes.
2. Each class calls 2 stored procedures.
3. The struts action calls these threads.
4. Also, the struts action joins itself to each thread. This is done to ensure that we return to screen after all details have been fetched from screen.
5. Response times have been reduced after this implementation.
6. I have pasted the code sample. Its a skeleton code only as couldn't post the actual code. But i am hoping that the i am able to put my point forward.
7. I have the following queries and appreciate your review and feedback:
a. Post this implementation; how is the memory effected considering the number of created objects has risen?
b. Will the memory usage shoot up due to this implementation?

Please let me know if more inputs are required from my end.








Thanks,
Tarun Trehan (http://allzhere.in)
Tarun Trehan
Ranch Hand

Joined: Mar 16, 2012
Posts: 49

Anyone ???
Please post your reviews.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2321
    
  28

a.how is the memory effected considering the number of created objects has risen?
b. Will the memory usage shoot up due to this implementation?


It's hard to answer both these questions without knowing what's going on inside. I would encourage you to measure memory usage using a tool like JConsole or JVisualVM. If all you are doing is called Stored Procedures, you shouldn;t be creating too many Java objects, and the memory usage in your Java app shouldn't be impacted. Your database however will have to do the same amount of work in half the time, so there will be double the load on the database. You might want to use database monitoring tools to monitor the load on the database server

Somethings that you might want to consider

a) Are the 2 sets of SPs updating the same tables? if Yes, have you considered if database locking would affect your performance
b) Starting threads is an expensive operation for Java. You are fine if you are executing this call once in a blue moon. However, if you are going to do this operation very often, you will be better off using a thread pool. It will be very easy for you to switch your implementation to a thread pool
c) How long are these stored procedures taking? From a usability POV, it is not advisable to make a HTTP request wait for more than 5 seconds. 5 seconds is an eternity for the user who has to sit there and wait for the browser spin. a better option is to start the backgroudn processing in the HTTP request, and return the request back with a message "Please wait while we process your request". Look at how some of the major travel sites do it.
Carles Gasques
Ranch Hand

Joined: Apr 19, 2013
Posts: 199
    
    1
Hi,

I understand that the number of objects that your tasks require have not been increased but now they are instantiated in parallel.

You have to test and verify if your jvm memory parameters are enough to accomplish the task without run in an out of memory exception.

For example use Java VisualVM to monitorize the app performance.


Cheers,
Tarun Trehan
Ranch Hand

Joined: Mar 16, 2012
Posts: 49

Jayesh & Carles,

Thanks for your reply.

Following is my response:

  • Jayesh: I have added code below to demonstrate what i am doing inside each thread call.
  • Jayesh: The DB load has been considered and its good to go and SP's are just fetching(SELECT) details from DB. User Wait is not an issue.
  • All, coming to point to use tools for monitoring; i do not have that option in my current organization. Explaining that would be a long task. I had used the following alternative for my self :
    http://allzhere.in/2013/02/16/java-memory-usage-monitoring-without-profiling-tools/.However that's not the concern here.


  • What i would like to know conceptually is:
  • Can there be a memory leak due to the attached code?
  • If not, then the only possible reason for memory increase or Out of memory should be higher load due to parallel processing. Right ?
  • I am not too sure about memory usage with threads and appreciate your inputs in that regard as well?




  • Carles Gasques
    Ranch Hand

    Joined: Apr 19, 2013
    Posts: 199
        
        1
    Hi Tarun,

    At first view your code will not produce a memory leak.
    You could run in a out of memory problem if the datasets are to large.


    Best regards,
    Tarun Trehan
    Ranch Hand

    Joined: Mar 16, 2012
    Posts: 49

    Carles,

    Thanks for your review.
    I will post my experience if i get to convince my organization to deploy a JAVA memory analyzer.

    Also posted it over my blog as well :

    JAVA Multithreading : A Practical Implementation Scenario
    Carles Gasques
    Ranch Hand

    Joined: Apr 19, 2013
    Posts: 199
        
        1
    Hi Tarun,

    I understand that your organization is aware that VisualVM, which is shipped in the jdk, and Eclipse MAT are free tools
    and there are other issues that prevent them to allow you to use such tools.


    Best regards,
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Review : MultiThreading Solution Implementation