Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The key to this question would depend on </p> <p>whether you intend to actually re-use a thread for running multiple tasks and thus maintain a pool of threads without terminating them [A] or </p> <p>whether you intend the pool only as a restriction to control the no of simultaneous tasks that can run at any given point and after each execution the thread would be killed and another one have to be created to take its place. [B]</p> <p>[B] can be achieved quite easily by returning a new Thread object if the total no of outstanding job requests are below your system limits. Obviously the thread object should know how to decrement the common resource usage counter, before exiting. In this case you would not have to maintain a collection of threads but simply a reference to the common reference counting object (eg a semaphore) from all the threads.</p> <p>[A] however, would require a class inherited from Thread which would not exit after it finishes executing a given task but would wait for the next task to be executed. Once this class is available, you can simply return the appropriate thread object reference to the caller and use a marker which will mark the thread as used or not in a thread safe way. </p> <p>Here is one simple way to achieve this </p> <pre><code>ArrayList&lt;MyThread&gt; pool = new ArrayList&lt;MyThread&gt; (DEFAULT_POOL SIZE); ArrayList&lt;AtomicBoolean&gt; used = new ArrayList&lt;AtomicBoolean&gt; (DEFAULT_POOL SIZE); </code></pre> <p>If you are returning pool[i] to the caller, mark it as being used, by performing </p> <pre><code>used[i].set(true). </code></pre> <p>Mark the same object as not being used (set(false) when the thread is returned to the pool. Obviously you would do this from synchronized allocate/deallocate methods. Since they are synchronized, the collection used to store the pooled threads needs not be synchronized and you can use any structure / collection, depending on the algorithm you use to determine which thread to give out next. </p> <p>As an optimization, you could create the pool (and the individual threads) on the fly at the first request for a pooled thread - that way you need not initialize it until it is available. But you need to do it in a thread safe manner, details of which i wont go into. </p> <p>As for question 2, it would be a good idea to make your pool class a singleton unless you want to create multiple pools, which would be dangerous if it went unchecked. It would also alleviate you from tracking how many such pools are instantiated if you are using a non static pool object. </p> <p>As indicated you could save yourself the trouble by re-using the existing thread pool classes which handled the scenario A quite nicely for you :) </p>
 

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