Server增加setThreads方法,可以动态修改线程池的大小

This commit is contained in:
Redkale
2017-10-22 15:39:00 +08:00
parent 8fcd33b511
commit f6c7dde28f
4 changed files with 14 additions and 7 deletions

View File

@@ -71,7 +71,7 @@ public abstract class Server<K extends Serializable, C extends Context, R extend
protected int threads;
//线程池
protected ExecutorService executor;
protected ThreadPoolExecutor executor;
//ByteBuffer池大小
protected int bufferPoolSize;
@@ -113,7 +113,7 @@ public abstract class Server<K extends Serializable, C extends Context, R extend
final AtomicInteger counter = new AtomicInteger();
final Format f = createFormat();
final String n = name;
this.executor = Executors.newFixedThreadPool(threads, (Runnable r) -> {
this.executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(threads, (Runnable r) -> {
Thread t = new WorkThread(executor, r);
t.setName(n + "-ServletThread-" + f.format(counter.incrementAndGet()));
return t;
@@ -166,6 +166,13 @@ public abstract class Server<K extends Serializable, C extends Context, R extend
return this.context;
}
public void setThreads(int threads) {
int oldthreads = this.threads;
this.context.executor.setCorePoolSize(threads);
this.threads = threads;
logger.info("[" + Thread.currentThread().getName() + "] " + this.getClass().getSimpleName() + " change threads from " + oldthreads + " to " + threads);
}
@SuppressWarnings("unchecked")
public void addServlet(S servlet, final Object attachment, AnyValue conf, K... mappings) {
this.prepare.addServlet(servlet, attachment, conf, mappings);