# 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.
|
|
|
|
| 5 | 5 | import java.util.concurrent.BlockingQueue; |
| 6 | 6 | import java.util.concurrent.LinkedBlockingQueue; |
| 7 | 7 | import java.util.concurrent.TimeUnit; |
| | 8 | import java.util.concurrent.atomic.AtomicInteger; |
| 8 | 9 | |
| 9 | 10 | /** |
| 10 | 11 | * A generic class that processes a list of {@link Runnable} one-by-one using |
| … |
… |
|
| 45 | 46 | /** |
| 46 | 47 | * Total number of worker threads currently idle or active |
| 47 | 48 | */ |
| 48 | | protected int workerThreadCount = 0; |
| | 49 | protected AtomicInteger workerThreadCount = new AtomicInteger(); |
| 49 | 50 | |
| 50 | 51 | /** |
| 51 | 52 | * Number of worker threads currently idle |
| 52 | 53 | */ |
| 53 | | protected int workerThreadIdleCount = 0; |
| | 54 | protected AtomicInteger workerThreadIdleCount = new AtomicInteger(); |
| 54 | 55 | |
| 55 | 56 | /** |
| 56 | 57 | * Just an id for identifying an worker thread instance |
| … |
… |
|
| 67 | 68 | public void addJob(Runnable job) { |
| 68 | 69 | try { |
| 69 | 70 | jobQueue.put(job); |
| 70 | | if (workerThreadIdleCount == 0 && workerThreadCount < WORKER_THREAD_MAX_COUNT) |
| | 71 | if (workerThreadIdleCount.get() == 0 && workerThreadCount.get() < WORKER_THREAD_MAX_COUNT) |
| 71 | 72 | addWorkerThread(); |
| 72 | 73 | } catch (InterruptedException e) { |
| 73 | 74 | } |
| … |
… |
|
| 75 | 76 | |
| 76 | 77 | protected JobThread addWorkerThread() { |
| 77 | 78 | JobThread jobThread = new JobThread(++workerThreadId); |
| 78 | | synchronized (this) { |
| 79 | | workerThreadCount++; |
| 80 | | } |
| | 79 | workerThreadCount.incrementAndGet(); |
| 81 | 80 | jobThread.start(); |
| 82 | 81 | return jobThread; |
| 83 | 82 | } |
| … |
… |
|
| 96 | 95 | @Override |
| 97 | 96 | public void run() { |
| 98 | 97 | executeJobs(); |
| 99 | | synchronized (instance) { |
| 100 | | workerThreadCount--; |
| | 98 | workerThreadCount.decrementAndGet(); |
| 101 | 99 | } |
| 102 | | } |
| 103 | 100 | |
| 104 | 101 | protected void executeJobs() { |
| 105 | 102 | while (!isInterrupted()) { |
| 106 | 103 | try { |
| 107 | | synchronized (instance) { |
| 108 | | workerThreadIdleCount++; |
| 109 | | } |
| | 104 | workerThreadIdleCount.incrementAndGet(); |
| 110 | 105 | if (firstThread) |
| 111 | 106 | job = jobQueue.take(); |
| 112 | 107 | else |
| … |
… |
|
| 114 | 109 | } catch (InterruptedException e1) { |
| 115 | 110 | return; |
| 116 | 111 | } finally { |
| 117 | | synchronized (instance) { |
| 118 | | workerThreadIdleCount--; |
| | 112 | workerThreadIdleCount.decrementAndGet(); |
| 119 | 113 | } |
| 120 | | } |
| 121 | 114 | if (job == null) |
| 122 | 115 | return; |
| 123 | | try { |
| 124 | 116 | job.run(); |
| 125 | 117 | job = null; |
| 126 | | } catch (Exception e) { |
| 127 | | e.printStackTrace(); |
| 128 | 118 | } |
| 129 | 119 | } |
| 130 | 120 | } |
| 131 | | } |
| 132 | 121 | |
| 133 | 122 | } |