On Mon, Feb 12, 2001 at 01:41:58AM -0500, Gianni Johansson wrote:
> On Sunday 11 February 2001 20:24, Scott wrote:
> 
> OK.  There's still a bug, but it didn't quite describe it right.  Look at the 
> implementation for runningThreads:
> 
>  private int runningThreads() {
>       return maxThreads-threads.size();
>     }

> 
> You are depending on the size of threads to keep track of how many
> threads are running.  But that would only make sense if you put the used
> EThreads back into threads in reclaim() which you don't do.

Yeah, that seems right.  There should be a ThreadGroup containing the
threads, and runningThreads should be the number of active threads in that
group minus those in the thread pool.

Attached is a patch.  I've updated the experimental tree if someone can
patch this against the stable tree.

        Scott


-------------- next part --------------
--- Freenet-old/thread/ThreadPool.java  Tue Jan 16 00:34:19 2001
+++ Freenet/thread/ThreadPool.java      Mon Feb 12 09:30:58 2001
@@ -30,6 +30,7 @@
  */
 public class ThreadPool extends Thread implements ThreadManager {
     protected int maxThreads, maxJobs, maxPoolThreads; 
+    protected ThreadGroup poolGroup;
     protected BlockingQueue jobs;
     protected BlockingStack threads;
     protected boolean run=true;
@@ -52,7 +53,8 @@
      * Creates a ThreadPool with a fixed number of concurrently
      * executable jobs.  This also allocates a job queue, so that
      * an unlimited number of new jobs can be inserted in a non-blocking
-     * manner even if all maxThreads threads are occupied.
+     * manner even if all
+     * maxThreads threads are occupied.
      *
      * @param tg      The ThreadGroup to use for the pool and all it's 
      * children.
@@ -68,8 +70,8 @@
      * Creates a ThreadPool with a fixed number of concurrently
      * executable jobs.  This also allocates a job queue, so that
      * new jobs can be inserted in a non-blocking manner even if all
-     * maxThreads threads are occupied. This ThreadPool allows up to a 
-     * total of maxJobs running and queued jobs.
+     * maxThreads threads are occupied. This ThreadPool allows up to a total 
of maxJobs
+     * running and queued jobs.
      *
      * @param maxThreads The number of jobs that can be active at any
      * one time.
@@ -77,7 +79,7 @@
      * run run()} returns false to turn away additional jobs past this point.
      */
     public ThreadPool(int maxPool, int maxThreads, int maxJobs) {
-       this(Thread.currentThread().getThreadGroup(),maxPool, maxThreads, 
+       this(new ThreadGroup("ThreadPool"),maxPool, maxThreads, 
             maxJobs);
     }

@@ -85,8 +87,8 @@
      * Creates a ThreadPool with a fixed number of concurrently
      * executable jobs.  This also allocates a job queue, so that
      * new jobs can be inserted in a non-blocking manner even if all
-     * maxThreads threads are occupied. This ThreadPool allows up to a 
-     * total of maxJobs running and queued jobs.
+     * maxThreads threads are occupied. This ThreadPool allows up to a total 
of maxJobs
+     * running and queued jobs.
      *
      * @param tg      The ThreadGroup to use for the pool and all it's 
      * children.
@@ -97,8 +99,9 @@
      */
     public ThreadPool(ThreadGroup tg, int maxPool, int maxThreads, 
                      int maxJobs) {
-       super(tg, "ThreadPool");
+       super("ThreadPool");
         setDaemon(true);
+       this.poolGroup=tg;
        jobs=new BlockingQueue();
        threads=new BlockingStack();
        this.maxPoolThreads=maxPool;
@@ -120,21 +123,16 @@
      *
      * @param e The EThread to reclaim.
      */
-    public boolean reclaim(EThread e) {
-       boolean rv=false;
-       if (maxThreads == 0 || threads.size()<maxPoolThreads) {
-           threads.push(e);
-           rv=true;
-       } 
-
+    public void reclaim(EThread e) {
+       if (maxThreads > 0 || threads.size()<maxPoolThreads)
+       threads.push(e);
        synchronized(threads) {
            threads.notifyAll();
        }
-       return rv;
     }

     private int runningThreads() {
-       return maxThreads-threads.size();
+       return poolGroup.activeCount()-threads.size();
     }

     /**
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 232 bytes
Desc: not available
URL: 
<https://emu.freenetproject.org/pipermail/devl/attachments/20010212/997f5ac4/attachment.pgp>

Reply via email to