This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Auto scaling using threadpool Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Auto scaling using threadpool" Watch "Auto scaling using threadpool" New topic
Author

Auto scaling using threadpool

sumit anand kumar
Ranch Hand

Joined: Apr 28, 2010
Posts: 83
Is there a way to do auto scaling of threadpool size.

I mean i was planning to use threadpool inside tomcat. And do some backend processing in a thread. I don't want to spawn a thread per request. I want to use a threadpool but want to scale the threadpool so that all requests should always get a thread from the threadpool
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2274
    
  28

You can set max pool size to a very high number, and your theads will grow to the max. BUT DON"T DO THAT!

Scaling a thread pool up based on number of requests defeats the purpose of a thread pool. The reason you have a threadpool is because
a) you don;t want to start/stop threads very often
b) under heavy load, you don't want to overwhelm the system.

When the number of requests that need to be run concurrently are more than the number of requests that you can handle, you don't want to start threads that will drive your resource usage to 100%. Once resource usage hits 100%(doesn't matter what kind.. CPU/memory/disk IO), there would be lot of overhead. If you are CPU bound, the CPU is going to go crazy switchig contexts. If you are memory bound, GC is going to go crazy. If you are IO bound, your disk is going to go crazy doing disk seeks. Bottom line is that resource constraints are hard limit in your system. You cannot magically make a system go faster than it can physical can by just starting more threads. It's a computer not a magic box You should always, always, always cap the max threads so that max resource utilization is 90%

The way a thread pool works is that when number of requests are greater than the max pool size, it will queue the requests. If you have 1000 requests and 10 threads, then the 10 threads will work on 10 requests, and the other 990 will get queued up. This is always much better than starting 1000 threads.
sumit anand kumar
Ranch Hand

Joined: Apr 28, 2010
Posts: 83
Jayesh A Lalwani wrote:
Scaling a thread pool up based on number of requests defeats the purpose of a thread pool. The reason you have a threadpool is because
a) you don;t want to start/stop threads very often
b) under heavy load, you don't want to overwhelm the system.


I understand the concept of thread pool. But I am building the application for a Amazon EC2 instance that supports autoscaling. Thats y i wanted to utilize this autoscaling feature.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2274
    
  28

If you are autoscaling by spawning machines on the cloud, why do you need to increase the number of threads? Each Amazon EC2 instance should be running a fixed number of worker threads, and you just scale up by starting new instances.

sumit anand kumar
Ranch Hand

Joined: Apr 28, 2010
Posts: 83
Thanks, but i will wait for a second opinion

Just thinking if what i am asking is conventionally not possible, can i create a homegrown threadpool where if all the threadpool threads are busy, i myself create a new thread. Carter the request, once done, i will add this thread to the threadpool. Is something like this possible & how?
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2274
    
  28

You can do what you need to do by setting Maxpoolsize to Integer.MAX_VALUE to create an unbounded threadpool. This will make the Thread pool grow to the number of requests that you have, and when your load reduces, idle threads will stopped to bring the thread pool back down to core size

Alternatively, you can set maxPoolsize to something more reasonable, and then change maxPoolsize if you want to have finer control. If you do this, you will have to worry about setting maxPoolSize back
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Auto scaling using threadpool
 
Similar Threads
How to kill runnig thread using java?(not waiting)
JBoss Basic ThreadPool
Thread Issues
How can I determine when my Threads are complete?
Java multithreading