package gpro.common.New;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ThreadManager {
public static int MAX_THREAD_COUNT = 20;
public static int CORE_THREAD_COUNT = 10;
private static final Logger logger = LoggerFactory.getLogger(ThreadManager.class);
private static final BlockingQueue<Runnable> queueOfRunnables = new LinkedBlockingQueue<Runnable>();
private static class ProcessQueue implements Runnable {
public void run() {
while (true) {
try {
Runnable runme = (Runnable) queueOfRunnables.poll(10, TimeUnit.MILLISECONDS);
if (runme != null) {
executor.execute(runme);
}
} catch (Exception e) {
logger.warn(e.toString());
}
}
}
}
private static class AppThreadManager implements ThreadFactory {
private final ThreadGroup tg = new ThreadGroup(ThreadManager.class.getName());
public Thread newThread(Runnable r) {
tg.setDaemon(true);
Thread t = new Thread(tg, r);
t.setDaemon(true);
t.setName("gproHookThread");
return t;
}
}
/*
* private static final ExecutorService executor =
* Executors.newFixedThreadPool(MAX_THREAD_COUNT, newAppThreadManager());
* static { executor.execute(new ProcessQueue()); }
*/
private staticThreadPoolExecutor executor = new ThreadPoolExecutor(CORE_THREAD_COUNT, MAX_THREAD_COUNT,
3600L,TimeUnit.SECONDS, queueOfRunnables,
new AppThreadManager());
public static void setThreadPoolSize(int size) {
MAX_THREAD_COUNT = size <= CORE_THREAD_COUNT ? CORE_THREAD_COUNT : size;
executor.setMaximumPoolSize(MAX_THREAD_COUNT);
}
public static void assignThreadTo(Runnablerunnable) {
if (queueOfRunnables.size() > MAX_THREAD_COUNT * 2) {
logger.warn("Threadusage forcing queue to grow large... " + queueOfRunnables.size());
}
executor.execute(runnable);
}
public static void shutDown() {
executor.shutdown();
executor.shutdownNow();
}
private ThreadManager() {
}
}
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ThreadManager {
public static int MAX_THREAD_COUNT = 20;
public static int CORE_THREAD_COUNT = 10;
private static final Logger logger = LoggerFactory.getLogger(ThreadManager.class);
private static final BlockingQueue<Runnable> queueOfRunnables = new LinkedBlockingQueue<Runnable>();
private static class ProcessQueue implements Runnable {
public void run() {
while (true) {
try {
Runnable runme = (Runnable) queueOfRunnables.poll(10, TimeUnit.MILLISECONDS);
if (runme != null) {
executor.execute(runme);
}
} catch (Exception e) {
logger.warn(e.toString());
}
}
}
}
private static class AppThreadManager implements ThreadFactory {
private final ThreadGroup tg = new ThreadGroup(ThreadManager.class.getName());
public Thread newThread(Runnable r) {
tg.setDaemon(true);
Thread t = new Thread(tg, r);
t.setDaemon(true);
t.setName("gproHookThread");
return t;
}
}
/*
* private static final ExecutorService executor =
* Executors.newFixedThreadPool(MAX_THREAD_COUNT, newAppThreadManager());
* static { executor.execute(new ProcessQueue()); }
*/
private staticThreadPoolExecutor executor = new ThreadPoolExecutor(CORE_THREAD_COUNT, MAX_THREAD_COUNT,
3600L,TimeUnit.SECONDS, queueOfRunnables,
new AppThreadManager());
public static void setThreadPoolSize(int size) {
MAX_THREAD_COUNT = size <= CORE_THREAD_COUNT ? CORE_THREAD_COUNT : size;
executor.setMaximumPoolSize(MAX_THREAD_COUNT);
}
public static void assignThreadTo(Runnablerunnable) {
if (queueOfRunnables.size() > MAX_THREAD_COUNT * 2) {
logger.warn("Threadusage forcing queue to grow large... " + queueOfRunnables.size());
}
executor.execute(runnable);
}
public static void shutDown() {
executor.shutdown();
executor.shutdownNow();
}
private ThreadManager() {
}
}
No comments:
Post a Comment