java - How to find the number of runnables waiting to be executed -
is there way know @ given point in time how many runnables waiting executed executorservice. example, assuming loop invoking execute method faster runnable can complete , surplus accumulates, there anyway running count of accumulated runnables?
executorservice es = executors.newfixedthreadpool(50); while (test) { es.execute(new myrunnable()); }
is there way know @ given point in time how many runnables waiting executed executorservice.
yes. instead of using executors... calls, should instantiate own threadpoolexecutor. below executors.newfixedthreadpool(50) returning:
threadpoolexecutor threadpool = new threadpoolexecutor(50, 50, 0l, timeunit.milliseconds, new linkedblockingqueue<runnable>()); once have threadpoolexecutor, has number of getter methods give data on pool. number of outstanding jobs should be:
threadpool.getqueue().getsize(); also available threadpoolexecutor are:
getactivecount()getcompletedtaskcount()getcorepoolsize()getlargestpoolsize()getmaximumpoolsize()getpoolsize()gettaskcount()
if want throttle number of jobs in queue don't far ahead, should use bounded blockingqueue , rejectedexecutionhandler. see answer here: process large file http calls in java
Comments
Post a Comment