Skip to content

Instantly share code, notes, and snippets.

@lwahlmeier
Created August 23, 2017 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lwahlmeier/9cdd428501586c9b77bfaf85555304d8 to your computer and use it in GitHub Desktop.
Save lwahlmeier/9cdd428501586c9b77bfaf85555304d8 to your computer and use it in GitHub Desktop.
package org.threadly.util;
import java.util.concurrent.atomic.AtomicInteger;
import org.threadly.concurrent.PriorityScheduler;
import org.threadly.concurrent.SubmitterScheduler;
import org.threadly.concurrent.wrapper.KeyDistributedScheduler;
import org.threadly.concurrent.wrapper.limiter.SubmitterSchedulerLimiter;
public class CenteralThreadManager {
private static volatile CenteralThreadManager instance = null;
private final PriorityScheduler threadPool = new PriorityScheduler(1);
private final KeyDistributedScheduler keyedScheduler = new KeyDistributedScheduler(threadPool);
private final AtomicInteger currentPoolSize = new AtomicInteger(1);
private final AtomicInteger minPoolSize = new AtomicInteger(1);
private final AtomicInteger totalPoolThreads = new AtomicInteger(1);
private CenteralThreadManager() {}
public static CenteralThreadManager getInstance() {
CenteralThreadManager ctm = instance;
if(ctm == null) {
synchronized(CenteralThreadManagerTest.class) {
if(instance == null) {
instance = new CenteralThreadManager();
}
}
ctm = instance;
}
return ctm;
}
public PriorityScheduler getGlobalThreadPool() {
return threadPool;
}
public SubmitterScheduler getSubPool(int threadSize, int dedicatedSize) {
totalPoolThreads.addAndGet(threadSize);
int current = 0;
while((current= currentPoolSize.get()) < threadSize) {
if(currentPoolSize.compareAndSet(current, threadSize+1)){
threadPool.setPoolSize(threadSize+1);
}
}
minPoolSize.addAndGet(dedicatedSize);
return new WrapperPool(threadPool, threadSize, dedicatedSize);
}
public void releaseSubPool(SubmitterScheduler subPool) {
if(subPool instanceof WrapperPool) {
WrapperPool wp = (WrapperPool)subPool;
minPoolSize.addAndGet(-wp.dedicatedSize);
totalPoolThreads.addAndGet(-wp.size);
int current = 0;
while( (current= currentPoolSize.get()) > minPoolSize.get()) {
if(currentPoolSize.compareAndSet(current, minPoolSize.get())) {
threadPool.setPoolSize(minPoolSize.get());
}
}
}
}
public int getCurrentPoolSize() {
return currentPoolSize.get();
}
public int getTotalPoolThreads() {
return totalPoolThreads.get();
}
public int getMinPoolSize() {
return minPoolSize.get();
}
private static class WrapperPool extends SubmitterSchedulerLimiter {
private final int size;
private final int dedicatedSize;
WrapperPool(PriorityScheduler threadPool, int size, int dedicatedSize) {
super(threadPool, size);
this.size = size;
this.dedicatedSize = dedicatedSize;
}
@Override
protected void finalize() {
CenteralThreadManager.getInstance().releaseSubPool(this);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment