Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

multithread in servlet container

 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inside servlet container, if I start multithread in service() method, it will improve performance ? what are the best practices ?

Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unlikely. Best practice is to construct your servlets intelligently.

What performance issue are you trying to solve?
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:What performance issue are you trying to solve?


Thinking about this bank application. A user place trade order, then in server side, we need to do these jobs
1. check user user name/ password
2. check user account amount
3. check user credit history
....

For performance, we can do these job in parallel, then for each request , we will create three threads to handle this order.

Then the problem is coming in. Inside the servlet container, multithread will be allowed and effective ?

Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are any of these operations lengthy enough to warrant all that?

I've worked on major banking application sites and we never resorted to anything like this.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edward Chen wrote:
Bear Bibeault wrote:What performance issue are you trying to solve?


Thinking about this bank application. A user place trade order, then in server side, we need to do these jobs
1. check user user name/ password
2. check user account amount
3. check user credit history
....

For performance, we can do these job in parallel, then for each request , we will create three threads to handle this order.

Then the problem is coming in. Inside the servlet container, multithread will be allowed and effective ?

Thanks.


Just a point, you'd not want to perform 2nd & 3rd if username & password is invalid or would you?
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:I've worked on major banking application sites and we never resorted to anything like this.


Here is just an example, not for a real world.

But in the real project, it is common that we have lots of logic inside one method, then one way to improve performance is to do them in parallel.
 
Anirvan Majumdar
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you insist, yes - you might improve performance [after all, at the end of the day it'll depend upon what kind of hardware you use and what kind of thread prioritisation and handling the OS does]. But inadvertently you might be working towards a *much* bigger overhead of thread management. Considering the importance of maintaining data integrity in banking transactions, you'll have to put in some more complex logic to ensure that shared data is properly handled. Typically, it would be more appropriate to club together the discrete scenarios you mentioned as part of one use-case. Most of the discrete scenarios might be dependent on each other, like Ankur mentioned. In that case, if you have threads, you'll also need to cater to inter-thread communication.
Before contemplating usage of threads, it's always wiser to see if the task can be done without them. The performance benefit you see may just not be worth the effort.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13077
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your service only gets one or two requests "at the same time" - sure, you might speed things up with parallel operations but suppose you become popular and have to handle 100 requests "at the same time" - then those extra Threads just chew up memory and cpu time.

Much better (IMHO) to think about intelligent architecture, caching, distributed processing, etc. than firing up lots of threads on your server.

Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic