[jboss-dev-forums] [Design the new POJO MicroContainer] - Re: Declaration and deployment of threads and thread pools i

adrian@jboss.org do-not-reply at jboss.com
Thu Dec 4 06:14:57 EST 2008


"david.lloyd at jboss.com" wrote : ...and I've created one.  It makes sure that the queue submit unblocks after the worker thread dies.  The executor used in the test is basically a regular ThreadPoolExecutor with the same queue wrapper, except I wrap the wrapped queue again to add addtional blocking so that the queue unblocks right when the race would occur - when the worker thread from the first task exits leaving zero threads in the pool.
  | 
  | http://anonsvn.jboss.org/repos/sandbox/david.lloyd/jboss-threads/trunk/main/src/test/java/org/jboss/threads/ThreadPoolTestCase.java

You can't test a race condition by blocking.
Its a race condition because the thing is not threadsafe. 
At best, all you'll do is cause a deadlock in your test. ;-)

The only way to test for a race condition is to stress test something that is
likely to reproduce the problem (in this case it is very difficult because
it requires starving a thread of cpu longer than its idle time :-)

On your solution, I don't think it resolves the real issue?

The issue is that the 
* offer to the queue
* end previous runnable
* add a thread 
are not synchronized with each other.

i.e. there are gaps between the addWorker() check and the offer to the queue
where the thread that addWorker() thinks exists
gets ended due to idleness leaving the Runnable in limbo.
Only a new call to addWorker() would recheck the active threads and spot
that there's no threads to execute the runnable.

So another workaround (besides the minimum of 1 threads) would be to introduce
a periodic redo of the addWorker() check.

Or put another way, whether you block in the queue.offer() or the rejection() 
won't make any real difference as you can see from the openjdk execute() code
they are essentially in the same place

  |         if (isRunning(c) && workQueue.offer(command)) {
  |             int recheck = ctl.get();
  |             if (! isRunning(recheck) && remove(command))
  |                 reject(command);
  |             else if (workerCountOf(recheck) == 0)
  |                 addWorker(null, false);
  |         }
  |         else if (!addWorker(command, false))
  |             reject(command);
  | 

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4194297#4194297

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4194297



More information about the jboss-dev-forums mailing list