title: Deadlocking /w ThreadPoolExecutor
body: <p>Summary: I'm trying to parallelize my system using ThreadPoolExecutor and I'm running into a deadlock issue. I know WHY I'm getting the issue, but I'm not very familiar with concurrency in context of java so I'm not sure what classes/methods to use:</p>
  
  <p>Simple Example: My code parses a large xml-like tree, and then spawns a thread for the top level instruction. This thread runs; as it executes, it reads more of the xml tree and spawns more threads on the same ThreadPoolExecutor (I use 1 pool because there is a user-defined maximum). This happens until the xml tree is consumed. </p>
  
  <p>When enough parent threads end up in the threadpool and all their child threads are still in the queue, they deadlock (because the parent threads are blocked on their children threads, who aren't running). Let's say my tree is</p>
  
  <pre><code>Parent thread
   Child Thread A
    Child Child Thread A1
    Child Child Thread A2
    Child Child Thread A3
   Child Thread B
   Child Thread C
  </code></pre>
  
  <p>I will now have the Parent thread, A, B, C, A1, A2, and A3 as threads. Let's assume my max threadpool is 2. This means that in the way it was parsed, P, A are in the thread pool and everything else is in the thread queue.</p>
  
  <p>Here, every non leaf node will be blocked on the leaf nodes. Since the leaf nodes were parsed last, they aren't even in the threadpool (and instead in the queue). Thusly the leaf children never run, and the whole system is deadlocked.</p>
  
  <p>Question: Is there some method call to kick the parent thread out of the running threadpool as soon as it creates child threads? I'm wary of using callbacks because my system can initiate concurrency optionally, so I don't really want to clutter code on the children classes to call back on a parent thread that may not even exist. </p>
  
  <p>Also, how should I go about implementing some sort of thread queue picking algorithm (priority/clock/etc); is ThreadPoolExecutor the right class to be using? </p>
  
  <p>Thanks. </p>