By default threads are not reused. Thread reuse seems efficient, so why not make it the default?
To answer this, consider a situation similar to the one we've just seen:
Now, here's something we've not mentioned: the node is so protective towards its netlets, that if A calls B and B takes too long to respond then the call will timeout--netlet A will get a TimeoutException (see package org.jtrix.project.libjtrix.sync). This is very nice of the node, because netlet A doesn't want to hang just because someone else's netlet is poorly written.
Back to our example: A calls B and B calls A, which is reusing its thread A1. Now suppose at this point thread A1 gets into a deadlock, or an infinite ``while'' loop, or for some other reason doesn't return. Then thread B1 will get a TimeoutException and, assuming B1 doesn't catch this, the same exception gets percolated back to A1, the original calling thread. But thread A1 is locked up, so it never receives that exception; it just carries on in its deadlock.
What's happened is that netlet A has been caught out by its own problematic code. The original call was from thread A1, thinking it was calling netlet B. But netlet B had to speak to netlet A which had a deadlock problem. Netlet B escaped from this deadlock thanks to the node's timeout protection, but netlet A was not so lucky.
The solution here is that A should not have been reusing threads. If A was not reusing threads then the call from B to A would have started a new thread, A2. Thread A2 would still have got deadlocked, but at least thread A1, being a different thread, would have found out about it.
This is why threads are not reused by default: because it's safer. Once you have thoroughly tested your code then you may choose to reuse threads.
Nik Silver 2002-03-09