views:

364

answers:

2

Hello,

I would be greatfull for help regarding my question. What is the rough "cost" of using threads in java? Are the any rule of thumbs/empirical values, how much memory the creation of one thread costs. (This is my main question). Furthermore is there a rough estimate how many "cpu cycles" the creation of a thread costs?

The motivation for this question is, in a servlet of a webapplication I want to parallelize the content creation as parts of the content are file based, db based as well as webservices based. But this would mean, that for every "http-request-thread" (of my serlvet container) I will have 2-4 further threads. (I will be using the ExecutorService in Java 6)...

What are your experiences in using "hunderts up to thousands" of java threads on a web-server?

Thank you very much! markus

+4  A: 

Each thread has its own stack, and consequently there's an immediate memory impact. The default thread stack size is (IIRC) 512k (adjustable using the -Xss option). Consequently using hundreds of threads will have an impact on the memory the VM consumes (quite possibly before any CPU impact unless those threads are running).

I've seen clients run into problems related to threads/memory, since it's not an obvious link. It's trivial to create 100,000 threads (using executors/pools etc.) and memory problems don't appear to be immediately attributable to this.

If you're servicing many clients, you may want to take a look at the Java NIO API and in particular multiplexing, which allows asynchronous network programming. That will permit you to handle many clients with only one thread, and consequently reduce your requirement for a huge number of threads.

Brian Agnew
Hello Brian Agnew. Your answer was very helpfull. Thanks!
Markus
+1  A: 

That depends: It depends on the OS, the Java version, and the CPU. The only way to figure this out is to try it and measure the results.

Since you'll be using the ExecutorService, it will be simple to control the number of threads. Don't use too few or requests will stack up. If you use too many, you'll run into performance problems with your file system and the DB long before Java runs out of threads.

Aaron Digulla