Skip to content

Commit

Permalink
Ported QueueingThreadPoolExecutor synchronization improvements from E…
Browse files Browse the repository at this point in the history
…clipse Smarthome (jupnp#96)

* Ported synchronization improvements from eclipse-archived/smarthome#5113

Signed-off-by: velichko.stoykov <[email protected]>
  • Loading branch information
velichkos authored and kaikreuzer committed Jun 26, 2018
1 parent 92ff7a7 commit 6c96b8f
Showing 1 changed file with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -58,16 +60,17 @@
*/
public class QueueingThreadPoolExecutor extends ThreadPoolExecutor {

private Logger logger = LoggerFactory.getLogger(QueueingThreadPoolExecutor.class);
private final Logger logger = LoggerFactory.getLogger(QueueingThreadPoolExecutor.class);

/** we will use a core pool size of 1 since we allow to timeout core threads. */
final static int CORE_THREAD_POOL_SIZE = 1;

/** Our queue for queueing tasks that wait for a thread to become available */
private LinkedTransferQueue<Runnable> taskQueue = new LinkedTransferQueue<>();
private final BlockingQueue<Runnable> taskQueue = new LinkedTransferQueue<>();

/** The thread for processing the queued tasks */
private Thread queueThread;
private final ReadWriteLock lock = new ReentrantReadWriteLock(true);

final private Object semaphore = new Object();

Expand Down Expand Up @@ -109,18 +112,30 @@ public static QueueingThreadPoolExecutor createInstance(String name, int threadP
* @param runnable the task to add
*/
protected void addToQueue(Runnable runnable) {
taskQueue.add(runnable);
lock.readLock().lock();

if (queueThread == null || !queueThread.isAlive()) {
synchronized (this) {
lock.readLock().unlock();
lock.writeLock().lock();
try {
// check again to make sure it has not been created by another thread
if (queueThread == null || !queueThread.isAlive()) {
logger.warn("Thread pool '{}' exhausted, queueing tasks now.", threadPoolName);
logger.info("Thread pool '{}' exhausted, queueing tasks now.", threadPoolName);
queueThread = createNewQueueThread();
queueThread.start();
}

lock.readLock().lock();
} finally {
lock.writeLock().unlock();
}
}

try {
taskQueue.add(runnable);
} finally {
lock.readLock().unlock();
}
}

@Override
Expand Down Expand Up @@ -181,7 +196,15 @@ public void run() {
logger.debug("Executing queued task of thread pool '{}'.", threadPoolName);
QueueingThreadPoolExecutor.super.execute(runnable);
} else {
break;
lock.writeLock().lock();
try {
if (taskQueue.isEmpty()) {
queueThread = null;
break;
}
} finally {
lock.writeLock().unlock();
}
}
} catch (InterruptedException e) {
}
Expand Down

0 comments on commit 6c96b8f

Please sign in to comment.