[concurrency-interest] ThreadPoolExecutor with corePoolSize as Zero

Rowlands, Ben (IT) Ben.Rowlands at morganstanley.com
Tue Dec 18 12:45:04 EST 2007


I also encountered this recently (some of the TPE configuration can be a
bit tricky). There are many posts on this is you Google around, for
example: 

  http://osdir.com/ml/java.jsr.166-concurrency/2006-12/msg00081.html

The javadocs are very clear as to when threads will be created - and it
depends on the current pool size relative to the core and max sizes.

Note, core threads are only created on demand so you could just create a
pool with > 0 core thread(s). An idle thread is unlikely to impact
performance significantly. In JDK/6 (or if using latest backport.j.u.c)
you can allow core threads to timeout so they won't stick around if
there is nothing to do:

  ThreadPoolExecutor#allowCoreThreadsToTimeout( true );

Interestingly, if you run your code using the j.u.c implementation
bundled with JDK/6 it *does* work - it appears to start a new thread
before the queue is full. I never managed to track down the change that
fixed this - does anyone know what changed to cause this to work?.
 
Ben Rowlands
Morgan Stanley | Technology
20 Cabot Square | Canary Wharf | Floor 06
London, E14 4QW
Phone: +44 20 7677-4075
Ben.Rowlands at morganstanley.com

 


________________________________

	From: concurrency-interest-bounces at cs.oswego.edu
[mailto:concurrency-interest-bounces at cs.oswego.edu] On Behalf Of Moran
Avigdor
	Sent: 18 December 2007 15:16
	To: concurrency-interest at cs.oswego.edu
	Subject: [concurrency-interest] ThreadPoolExecutor with
corePoolSize as Zero
	
	

	When initializing a thread pool with a core-pool-size of Zero I
see that no threads execute the runnable tasks.

	Looking at this further it seems that only if the queue rejected
the offer, then a first thread will be created.

	 

	A ThreadPoolExecutor with a SynchronousQueue() will work - but a
LinkedBlockingQueue() will not.
	Is this considered a known limitation? Must I initialize my pool
with corePoolSize as 1 with an unbounded queue?

	 

	ThreadPoolExecutor pool = new ThreadPoolExecutor(0, 10, 60L,
TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());

	            

	for (int i=0; i<10; ++i)

	{

	      pool.execute(new Runnable() {

	            public void run() {

	                  System.out.println("running");

	            };

	});

	}

	 

	Thanks.
--------------------------------------------------------

NOTICE: If received in error, please destroy and notify sender. Sender does not intend to waive confidentiality or privilege. Use of this email is prohibited when received in error.



More information about the Concurrency-interest mailing list