Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Java thread creation is expensive because there is a fair bit of work involved:</p> <ul> <li>A large block of memory has to be allocated and initialized for the thread stack.</li> <li>System calls need to be made to create / register the native thread with the host OS.</li> <li>Descriptors needs to be created, initialized and added to JVM internal data structures.</li> </ul> <p>It is also expensive in the sense that the thread ties down resources as long as it is alive; e.g. the thread stack, any objects reachable from the stack, the JVM thread descriptors, the OS native thread descriptors.</p> <p>The costs of all of these things are platform specific, but they are not cheap on any Java platform I've ever come across.</p> <hr> <p>A Google search found me an <a href="http://www.mail-archive.com/java-linux@java.blackdown.org/msg15146.html" rel="noreferrer">old benchmark</a> that reports a thread creation rate of ~4000 per second on a Sun Java 1.4.1 on a 2002 vintage dual processor Xeon running 2002 vintage Linux. A more modern platform will give better numbers ... and I can't comment on the methodology ... but at least it gives a ballpark for <em>how expensive</em> thread creation is likely to be.</p> <p>Peter Lawrey's benchmarking indicates that thread creation is significantly faster these days in absolute terms, but it is unclear how much of this is due improvements in Java and/or the OS ... or faster processor speeds. But his numbers <em>still</em> indicate a 150+ fold improvement if you use a thread pool versus creating/starting a new thread each time. (And he makes the point that this is all relative ...)</p> <hr> <p>(The above assumes "native threads" rather than "green threads", but modern JVMs all use native threads for performance reasons. Green threads are possibly cheaper to create, but you pay for it in other areas.)</p> <hr> <p>I've done a bit of digging to see how a Java thread's stack really gets allocated. In the case of OpenJDK 6 on Linux, the thread stack is allocated by the call to <code>pthread_create</code> that creates the native thread. (The JVM does not pass <code>pthread_create</code> a preallocated stack.)</p> <p>Then, within <code>pthread_create</code> the stack is allocated by a call to <code>mmap</code> as follows:</p> <pre><code>mmap(0, attr.__stacksize, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) </code></pre> <p>According to <code>man mmap</code>, the <code>MAP_ANONYMOUS</code> flag causes the memory to be initialized to zero.</p> <p>Thus, even though it might not be essential that new Java thread stacks are zeroed (per the JVM spec), in practice (at least with OpenJDK 6 on Linux) they are zeroed.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload