On Wednesday 08 April 2009 06:05:10 [email protected] wrote:
> Author: j16sdiz
> Date: 2009-04-08 05:05:10 +0000 (Wed, 08 Apr 2009)
> New Revision: 26639
> 
> Modified:
>    trunk/freenet/src/freenet/support/PrioritizedSerialExecutor.java
> Log:
> Keep job counts

Why is this faster than calling .size() ?
> 
> Modified: trunk/freenet/src/freenet/support/PrioritizedSerialExecutor.java
> ===================================================================
> --- trunk/freenet/src/freenet/support/PrioritizedSerialExecutor.java  
2009-04-08 05:04:47 UTC (rev 26638)
> +++ trunk/freenet/src/freenet/support/PrioritizedSerialExecutor.java  
2009-04-08 05:05:10 UTC (rev 26639)
> @@ -11,6 +11,8 @@
>  public class PrioritizedSerialExecutor implements Executor {
>       
>       private final LinkedList<Runnable>[] jobs;
> +     private final int[] jobCount;
> +     
>       private final int priority;
>       private final int defaultPriority;
>       private boolean waiting;
> @@ -117,7 +119,11 @@
>                                       if(!jobs[i].isEmpty()) {
>                                               
> if(Logger.shouldLog(Logger.MINOR, this))
>                                                       Logger.minor(this, 
> "Chosen job at priority "+i);
> -                                             return (Runnable) 
> jobs[i].removeFirst();
> +                                             synchronized (jobs) {
> +                                                     Runnable r = 
> jobs[i].removeFirst();
> +                                                     jobCount[i]--;
> +                                                     return r;
> +                                             }
>                                       }
>                               }
>                       } else {
> @@ -125,7 +131,11 @@
>                                       if(!jobs[i].isEmpty()) {
>                                               
> if(Logger.shouldLog(Logger.MINOR, this))
>                                                       Logger.minor(this, 
> "Chosen job at priority "+i);
> -                                             return (Runnable) 
> jobs[i].removeFirst();
> +                                             synchronized (jobs) {
> +                                                     Runnable r = 
> jobs[i].removeFirst();
> +                                                     jobCount[i]--;
> +                                                     return r;
> +                                             }
>                                       }
>                               }
>                       }
> @@ -145,6 +155,7 @@
>               jobs = new LinkedList[internalPriorityCount];
>               for(int i=0;i<jobs.length;i++)
>                       jobs[i] = new LinkedList<Runnable>();
> +             jobCount = new int[internalPriorityCount];
>               this.priority = priority;
>               this.defaultPriority = defaultPriority;
>               this.invertOrder = invertOrder;
> @@ -191,6 +202,7 @@
>                       if(logMINOR) 
>                               Logger.minor(this, "Running "+jobName+" : 
> "+job+" priority "+prio+" 
running="+running+" waiting="+waiting);
>                       jobs[prio].addLast(job);
> +                     jobCount[prio]++;
>                       jobs.notifyAll();
>                       if(!running && realExecutor != null) {
>                               reallyStart(logMINOR);
> @@ -209,6 +221,7 @@
>                               return;
>                       }
>                       jobs[prio].addLast(job);
> +                     jobCount[prio]++;
>                       jobs.notifyAll();
>                       if(!running && realExecutor != null) {
>                               reallyStart(logMINOR);
> @@ -245,17 +258,16 @@
>       }
>  
>       public int[] runningJobs() {
> -             int[] retval = new int[jobs.length];
> +             int[] retval;
>               synchronized(jobs) {
> -                     for(int i=0;i<retval.length;i++)
> -                             retval[i] = jobs[i].size();
> +                     retval = jobCount.clone();
>               }
>               return retval;
>       }
>  
>       public int getQueueSize(int priority) {
>               synchronized(jobs) {
> -                     return jobs[priority].size();
> +                     return jobCount[priority];
>               }
>       }
>  
> 
> _______________________________________________
> cvs mailing list
> [email protected]
> http://emu.freenetproject.org/cgi-bin/mailman/listinfo/cvs
> 
> 


Attachment: signature.asc
Description: This is a digitally signed message part.

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

Reply via email to