multithreading - Java ThreadPoolExecutor and BlockingQueue to be used -


in oracle site (http://docs.oracle.com/javase/7/docs/api/?java/util/concurrent/threadpoolexecutor.html) under queuing section, mentioned "if corepoolsize or more threads running, executor prefers queuing request rather adding new thread."

lot of our code have corepoolsize 1 , maximumpoolsize 10 or 20. (1) bad practice? or rather not optimal way use threadpool's? (2) recommend have corepoolsize , maximumpoolsize value same when creating threadpoolexecutor?

another question respect usage of blockingqueue while creating threadpoolexecutor - between linkedblockingqueue or concurrentlinkedqueue? looking more locking , performance point-of-view? though running test proves, inserting in concurrentlinkedqueue quite fast compared other. thoughts?

edited:

the first part has been answered in various questions. 1 liked how threadpoolexecutor increase threads max before queueing?

please find bellow points .

  1. if new task submitted list of tasks executed, , less corepoolsize threads running, new thread created handle request. incoming tasks queued in case corepoolsize or more threads running.

  2. if request can’t queued or there less corepoolsize threads running, new thread created unless exceed maximumpoolsize.

  3. if pool has more corepoolsize threads, excess threads terminated if have been idle more keepalivetime.

  4. if new task submitted list of tasks executed , there more corepoolsize less maximumpoolsize threads running, new thread created if queue full. means when using unbounded queue, no more threads corepoolsize running , keepalivetime has no influence.

  5. if corepoolsize , maximumpoolsize same, fixed-size thread pool used

    your threadpool size

    core pool size 1.

    max pool size 10 or 20.

question 1 : bad practice? or rather not optimal way use threadpool's?

it based on pool used.

if using bounded queue , new thread created upto max size once reaches max limit of bounded queue. otherwise run in core pool size . if using unbounded queue core pool size only.

question 2 : recommend have corepoolsize , maximumpoolsize value same when creating threadpoolexecutor?

yes. can same no of threads instead of kind blocking queue . non blocking queue can max no of threads.

question 3 concurrentlinkedqueue vs linkedblockingqueue

you should not allow use concurrentlinkedqueue since threadpool supports blockingqueue. may try concurrentlinkedblockingqueue. because concurrentlinkedblockingqueue unbounded. linkedblockingqueue bounded.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -