use of com.alibaba.dubbo.common.utils.NamedThreadFactory in project dubbo by alibaba.
the class MinaClient method doOpen.
@Override
protected void doOpen() throws Throwable {
connectorKey = getUrl().toFullString();
SocketConnector c = connectors.get(connectorKey);
if (c != null) {
connector = c;
} else {
// set thread pool.
connector = new SocketConnector(Constants.DEFAULT_IO_THREADS, Executors.newCachedThreadPool(new NamedThreadFactory("MinaClientWorker", true)));
// config
SocketConnectorConfig cfg = (SocketConnectorConfig) connector.getDefaultConfig();
cfg.setThreadModel(ThreadModel.MANUAL);
cfg.getSessionConfig().setTcpNoDelay(true);
cfg.getSessionConfig().setKeepAlive(true);
int timeout = getTimeout();
cfg.setConnectTimeout(timeout < 1000 ? 1 : timeout / 1000);
// set codec.
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MinaCodecAdapter(getCodec(), getUrl(), this)));
connectors.put(connectorKey, connector);
}
}
use of com.alibaba.dubbo.common.utils.NamedThreadFactory in project Hotchpotch by carryxyh.
the class EnhancedThreadPoolTest method testEnhancedThreadPool.
/**
* it print like this:
* thread number in current pool:1, task number in task queue:0 executor size: 1
* thread number in current pool:2, task number in task queue:0 executor size: 2
* thread number in current pool:3, task number in task queue:0 executor size: 3
* thread number in current pool:4, task number in task queue:0 executor size: 4
* thread number in current pool:5, task number in task queue:0 executor size: 5
* thread number in current pool:6, task number in task queue:0 executor size: 6
* thread number in current pool:7, task number in task queue:0 executor size: 7
* thread number in current pool:8, task number in task queue:0 executor size: 8
* thread number in current pool:9, task number in task queue:0 executor size: 9
* thread number in current pool:10, task number in task queue:0 executor size: 10
* thread number in current pool:10, task number in task queue:4 executor size: 10
* thread number in current pool:10, task number in task queue:3 executor size: 10
* thread number in current pool:10, task number in task queue:2 executor size: 10
* thread number in current pool:10, task number in task queue:1 executor size: 10
* thread number in current pool:10, task number in task queue:0 executor size: 10
*
* we can see , when the core threads are in busy , the thread pool create thread (but thread nums always less than max) instead of put task into queue.
*/
@Test
public void testEnhancedThreadPool() throws Exception {
String name = "enhanced-tf";
int queues = 5;
int cores = 5;
int threads = 10;
// alive 1 second
long alive = 1000;
URL url = new URL("dubbo", "localhost", 8080);
// init queue and enhanced executor
EnhancedTaskQueue<Runnable> enhancedTaskQueue = new EnhancedTaskQueue<Runnable>(queues);
final EnhancedThreadPoolExecutor executor = new EnhancedThreadPoolExecutor(cores, threads, alive, TimeUnit.MILLISECONDS, enhancedTaskQueue, new NamedThreadFactory(name, true), new AbortPolicyWithReport(name, url));
enhancedTaskQueue.setExecutor(executor);
for (int i = 0; i < 15; i++) {
Thread.sleep(50);
executor.execute(new Runnable() {
@Override
public void run() {
System.out.println("thread number in current pool:" + executor.getPoolSize() + ", task number in task queue:" + executor.getQueue().size() + " executor size: " + executor.getPoolSize());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
Thread.sleep(5000);
// cores theads are all alive
Assert.assertTrue("more than cores threads alive!", executor.getPoolSize() == cores);
}
Aggregations