T
Thomas Hawtin
Domagoj said:Has anyone measured how expensive creating threads really is in Java?
It is highly dependent upon JVM. IIRC, BEA JRockit takes very little
time to create and start a Thread (presumably they've made Thread a
facade over a thread-pool).
And how much memory does an Executor's thread pool consume?
About as much as a Thread multiplied by the number of threads.
I know that forking a new thread (basically a runnable) takes less
than a millisecond. I haven't measured how much resources does a
thread pool consume per thread, or how big can thread pool become
before thread switching starts taking too much time.
For computationally heavy threads, you generally want to avoid switches.
So you want approximately as many threads as you have hardware-threads
(Runtime.availableProcessors since 1.4). It's not just the actual switch
that costs, your caches will hurt immediately afterwards. Also any
thread-local pools will be duplicated, and you can have problems with
lock contention.
You can benchmark how scalable your application is by running it with
increasing numbers of threads. If it doesn't scale very well, you can
then look into it further. Or if it does scale well, you can do
something more productive instead.
Tom Hawtin