Author: nextgens
Date: 2009-01-27 14:42:27 +0000 (Tue, 27 Jan 2009)
New Revision: 25311

Modified:
   trunk/freenet/src/freenet/node/NodeStats.java
   trunk/freenet/src/freenet/support/Executor.java
   trunk/freenet/src/freenet/support/PooledExecutor.java
Log:
optimize NodeStats.getActiveThreadCount() further; review is welcome

Modified: trunk/freenet/src/freenet/node/NodeStats.java
===================================================================
--- trunk/freenet/src/freenet/node/NodeStats.java       2009-01-27 14:20:37 UTC 
(rev 25310)
+++ trunk/freenet/src/freenet/node/NodeStats.java       2009-01-27 14:42:27 UTC 
(rev 25311)
@@ -1014,12 +1014,7 @@
        }
 
        public int getActiveThreadCount() {
-               int waitingThreads = 0;
-               waitingThreadsByPriorities = node.executor.waitingThreads();
-               for(int i=0; i<waitingThreadsByPriorities.length; i++)
-                       waitingThreads += waitingThreadsByPriorities[i];
-               
-               return rootThreadGroup.activeCount() - waitingThreads;
+               return rootThreadGroup.activeCount() - 
node.executor.getWaitingThreadsCounter();
        }
        
        public int[] getActiveThreadsByPriority() {

Modified: trunk/freenet/src/freenet/support/Executor.java
===================================================================
--- trunk/freenet/src/freenet/support/Executor.java     2009-01-27 14:20:37 UTC 
(rev 25310)
+++ trunk/freenet/src/freenet/support/Executor.java     2009-01-27 14:42:27 UTC 
(rev 25311)
@@ -16,4 +16,7 @@
        public int[] waitingThreads();
        /** Count the number of threads running at each priority level */
        public int[] runningThreads();
+
+       /** Fast method returning how many threads are waiting */
+       public int getWaitingThreadsCounter();
 }

Modified: trunk/freenet/src/freenet/support/PooledExecutor.java
===================================================================
--- trunk/freenet/src/freenet/support/PooledExecutor.java       2009-01-27 
14:20:37 UTC (rev 25310)
+++ trunk/freenet/src/freenet/support/PooledExecutor.java       2009-01-27 
14:42:27 UTC (rev 25311)
@@ -22,6 +22,7 @@
        /** Threads waiting for a job */
        @SuppressWarnings("unchecked")
        private final ArrayList<MyThread>[] waitingThreads = new 
ArrayList[runningThreads.length];
+       private volatile int waitingThreadsCounter;
        long[] threadCounter = new long[runningThreads.length];
        private long jobCount;
        private long jobMisses;
@@ -39,6 +40,7 @@
                        waitingThreads[i] = new ArrayList<MyThread>();
                        threadCounter[i] = 0;
                }
+               waitingThreadsCounter = 0;
        }
        /** Maximum time a thread will wait for a job */
        static final int TIMEOUT = 5 * 60 * 1000;
@@ -68,6 +70,7 @@
                                jobCount++;
                                if(!waitingThreads[prio - 1].isEmpty()) {
                                        t = waitingThreads[prio - 
1].remove(waitingThreads[prio - 1].size() - 1);
+                                       waitingThreadsCounter--;
                                        if(logMINOR)
                                                Logger.minor(this, "Reusing 
thread " + t);
                                } else {
@@ -131,6 +134,10 @@
                return result;
        }
 
+       public int getWaitingThreadsCounter() {
+               return waitingThreadsCounter;
+       }
+
        class MyThread extends NativeThread {
 
                final String defaultName;
@@ -160,6 +167,7 @@
                                if(job == null) {
                                        synchronized(PooledExecutor.this) {
                                                waitingThreads[nativePriority - 
1].add(this);
+                                               waitingThreadsCounter++;
                                        }
                                        synchronized(this) {
                                                if(nextJob == null) {
@@ -177,6 +185,7 @@
                                        }
                                        synchronized(PooledExecutor.this) {
                                                waitingThreads[nativePriority - 
1].remove(this);
+                                               waitingThreadsCounter--;
                                                if(!alive) {
                                                        
runningThreads[nativePriority - 1].remove(this);
                                                        if(logMINOR)

_______________________________________________
cvs mailing list
[email protected]
http://emu.freenetproject.org/cgi-bin/mailman/listinfo/cvs

Reply via email to