java - ExecutorService that scales threads then queues tasks -


there executorservice implementation behaves thread pool following characteristics?

  1. there @ least x active threads.
  2. if task submitted , active threads busy, new thread started, y threads.
  3. if task submitted , y threads busy, task queued.
  4. if no new tasks submitted, pool scales down x active threads.

pretty standard thread pooling behavior. you'd think threadpoolexecutor handle this, but

executorservice = new threadpoolexecutor(     2, 10, // min/max threads     60, timeunit.seconds, // time of inactivity before scaling     new synchronousqueue<runnable>()); // task queue 

will throw exception if more 10 tasks submitted. switching linkedblockingqueue never scale past 2 minimum threads, unless limit size new linkedblockingqueue<runnable>(20), in case there 2 threads handling 1-20 tasks, 2-10 threads handling 21-30 tasks, , exception more 30 tasks. not pretty. fixed thread pool, meanwhile, never scale down inactive threads.

so, i'm after, can use different kind of blockingqueue or fiddle other setting i've missed? there exceutorservice implementation better suited (which?), or better off rolling own overriding execute() method of threadpoolexecutor?

unfortunately, answer no. best can in jre not have thread floor, ceiling. can accomplished allowing core threads timeout.

threadpoolexecutor tpe = new threadpoolexecutor(10, 10, 60, timeunit.seconds, new linkedblockingqueue<runnable>()); tpe.allowcorethreadtimeout(true); 

because core size 10, new thread started when task submitted until 10 threads active. after that, tasks queue in linkedblockingqueue. if thread has been inactive 60 seconds terminate.

the behavior want possible writing class implementing both blockingqueue , rejectedexecutionhandler, checks threadpoolexecutors current state before determining if task should added queue or rejected.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -