Ticket #7736: JobDispatcher.patch

File JobDispatcher.patch, 3.3 KB (added by Locked, 14 years ago)

patch for both changes

  • JobDispatcher.java

    # This patch file was generated by NetBeans IDE
    # Following Index: paths are relative to: C:\Users\Franz\Documents\NetBeansProjects\JOSM-Plugins\core\src\org\openstreetmap\gui\jmapviewer
    # This patch can be applied using context Tools: Patch action on respective folder.
    # It uses platform neutral UTF-8 encoding and \n newlines.
    # Above lines and this line are ignored by the patching process.
     
    55import java.util.concurrent.BlockingQueue;
    66import java.util.concurrent.LinkedBlockingQueue;
    77import java.util.concurrent.TimeUnit;
     8import java.util.concurrent.atomic.AtomicInteger;
    89
    910/**
    1011 * A generic class that processes a list of {@link Runnable} one-by-one using
     
    4546    /**
    4647     * Total number of worker threads currently idle or active
    4748     */
    48     protected int workerThreadCount = 0;
     49    protected AtomicInteger workerThreadCount = new AtomicInteger();
    4950
    5051    /**
    5152     * Number of worker threads currently idle
    5253     */
    53     protected int workerThreadIdleCount = 0;
     54    protected AtomicInteger workerThreadIdleCount = new AtomicInteger();
    5455
    5556    /**
    5657     * Just an id for identifying an worker thread instance
     
    6768    public void addJob(Runnable job) {
    6869        try {
    6970            jobQueue.put(job);
    70             if (workerThreadIdleCount == 0 && workerThreadCount < WORKER_THREAD_MAX_COUNT)
     71            if (workerThreadIdleCount.get() == 0 && workerThreadCount.get() < WORKER_THREAD_MAX_COUNT)
    7172                addWorkerThread();
    7273        } catch (InterruptedException e) {
    7374        }
     
    7576
    7677    protected JobThread addWorkerThread() {
    7778        JobThread jobThread = new JobThread(++workerThreadId);
    78         synchronized (this) {
    79             workerThreadCount++;
    80         }
     79        workerThreadCount.incrementAndGet();
    8180        jobThread.start();
    8281        return jobThread;
    8382    }
     
    9695        @Override
    9796        public void run() {
    9897            executeJobs();
    99             synchronized (instance) {
    100                 workerThreadCount--;
     98            workerThreadCount.decrementAndGet();
    10199            }
    102         }
    103100
    104101        protected void executeJobs() {
    105102            while (!isInterrupted()) {
    106103                try {
    107                     synchronized (instance) {
    108                         workerThreadIdleCount++;
    109                     }
     104                    workerThreadIdleCount.incrementAndGet();
    110105                    if (firstThread)
    111106                        job = jobQueue.take();
    112107                    else
     
    114109                } catch (InterruptedException e1) {
    115110                    return;
    116111                } finally {
    117                     synchronized (instance) {
    118                         workerThreadIdleCount--;
     112                    workerThreadIdleCount.decrementAndGet();
    119113                    }
    120                 }
    121114                if (job == null)
    122115                    return;
    123                 try {
    124116                    job.run();
    125117                    job = null;
    126                 } catch (Exception e) {
    127                     e.printStackTrace();
    128118                }
    129119            }
    130120        }
    131     }
    132121
    133122}