Note that there are some explanatory texts on larger screens.

plurals
  1. POThreadPoolExecutor Utility methods
    primarykey
    data
    text
    <p>I am writing a thread pool utility in my <code>multithreading</code> program. i just need to validate the following methods are correct and are they return the right values for me. i am using a <code>LinkedBlockingQueue</code> with size of 1. and also I refer to the java doc and it always says 'method will return <strong>approximate</strong>' number phrase. so i doubt weather following conditions are correct.</p> <pre><code>public boolean isPoolIdle() { return myThreadPool.getActiveCount() == 0; } public int getAcceptableTaskCount() { //initially poolSize is 0 ( after pool executes something it started to change ) if (myThreadPool.getPoolSize() == 0) { return myThreadPool.getCorePoolSize() - myThreadPool.getActiveCount(); } return myThreadPool.getPoolSize() - myThreadPool.getActiveCount(); } public boolean isPoolReadyToAcceptTasks(){ return myThreadPool.getActiveCount()&lt;myThreadPool.getCorePoolSize(); } </code></pre> <p>Please let me know your thoughts and suggestions.</p> <p>UPDATE </p> <p>interesting thing was if pool returns me there are 3 threads available from the <code>getAcceptableTaskCount</code> method and when i pass 3 tasks to the pool some times one task got rejected and it is handle by <code>RejectedExecutionHandler</code>. some times pool will handle all the tasks i passed. i am wondering why pool is rejected the tasks since i am passing tasks according to the available thread count. </p> <p>--------- implementation of the answer of gray---</p> <pre><code>class MyTask implements Runnable { @Override public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("exec"); } </code></pre> <p>}</p> <pre><code>@Test public void testTPool(){ ExecutorService pool = Executors.newFixedThreadPool(5); List&lt;Future&lt;MyTask&gt;&gt; list = new ArrayList&lt;Future&lt;MyTask&gt;&gt;(); for (int i = 0; i &lt; 5; i++) { MyTask t = new MyTask(); list.add(pool.submit(t, t)); } for (int i = 0; i &lt; list.size(); i++) { Future&lt;MyTask&gt; t = list.get(i); System.out.println("Result -"+t.isDone()); MyTask m = new MyTask(); list.add(pool.submit(m,m)); } } </code></pre> <p>This will print <code>Result -false</code> in the console meaning that task is not complete.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload