Thread reuse

The thread_reuse parameter of setConcurrency() gives us further control. This boolean tells the node whether it can reuse a waiting thread (true) or if all threads should be started as new (false). The default is no thread reuse (false).

For example, suppose netlet A calls a facet method from netlet B which, in order to complete, needs to call back to A. Then in the normal state of affairs there are three threads:

  1. Thread A1 is the initial calling thread. It runs in netlet A's processing space.
  2. The method call starts a thread B1 which is entirely within B's processing space. Thread B1 does not count against netlet A at all.
  3. Thread A2, which is called by thread B1 in netlet B, but which is a thread entirely within netlet A's space.
At this stage netlet A has two threads and B has one thread.

But this could be made more efficient if we were to enable thread reuse. Then when B1 calls netlet A it's the original thread, A1 which does the work. Thus A only uses one thread.

Nodality is quite smart about reusing threads. So in the above example the call sequence goes from A to B to A, then unwinds back to A when the last call is complete, and Nodality won't reuse a thread from a different calling sequence.

Thread reuse can also be helpful for avoiding deadlocks. Take the above A1-B1-A2 example with no reuse. Suppose thread A1 was the 10th thread and we had set a concurrency level of 10. Then B1 would wait for a free thread, which may never happen, thus creating a deadlock. If we had thread reuse then A1 could be reused and B1 could complete happily.

Thread reuse also preserves use of synchronize monitors. Take the situation A1-B1-A1 (thread reuse). If the first use of A1 held a synchronize monitor then the second use of A1 would still have that same monitor.

Nik Silver 2002-03-09