下面列出了java.nio.channels.AsynchronousChannelGroup#withFixedThreadPool() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void initAioProcessor(int processorCount) throws IOException {
for (int i = 0; i < processorCount; i++) {
asyncChannelGroups[i] = AsynchronousChannelGroup.withFixedThreadPool(processorCount,
new ThreadFactory() {
private int inx = 1;
@Override
public Thread newThread(Runnable r) {
Thread th = new Thread(r);
//TODO
th.setName(DirectByteBufferPool.LOCAL_BUF_THREAD_PREX + "AIO" + (inx++));
LOGGER.info("created new AIO thread " + th.getName());
return th;
}
}
);
}
}
@Override
public void listen(int thread, int port, AioServerListener listener) {
this.port = port;
this.listener = listener;
try {
channelGroup = AsynchronousChannelGroup.withFixedThreadPool(thread, Executors.defaultThreadFactory());
serverSocketChannel = AsynchronousServerSocketChannel.open(channelGroup);
serverSocketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
serverSocketChannel.bind(new InetSocketAddress(port));
serverSocketChannel.accept(null, this);
if (logger.isInfoEnable())
logger.info("启动AIO监听[{}]服务。", port);
} catch (IOException e) {
logger.warn(e, "启动AIO监听[{}]服务时发生异常!", port);
}
}
public void setUpHandlers() {
try {
AsynchronousChannelGroup asyncChannelGroup = AsynchronousChannelGroup.withFixedThreadPool(poolSize, Executors.defaultThreadFactory());
serverSocketChannel = AsynchronousServerSocketChannel.open(asyncChannelGroup).bind(new InetSocketAddress(listenPort));
serverSocketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
} catch (IOException e) {
e.printStackTrace();
}
log.info("** " + poolSize + " handler thread has been setup! **");
log.info("** Socket Server has been startup, listen port is " + listenPort + "! **");
}
public static void main(String[] args) throws Exception {
// create channel groups
ThreadFactory factory = new PrivilegedThreadFactory();
AsynchronousChannelGroup group1 = AsynchronousChannelGroup
.withFixedThreadPool(5, factory);
AsynchronousChannelGroup group2 = AsynchronousChannelGroup
.withCachedThreadPool(Executors.newCachedThreadPool(factory), 0);
AsynchronousChannelGroup group3 = AsynchronousChannelGroup
.withThreadPool(Executors.newFixedThreadPool(10, factory));
try {
// execute simple tasks
testSimpleTask(group1);
testSimpleTask(group2);
testSimpleTask(group3);
// install security manager and test again
System.setSecurityManager( new SecurityManager() );
testSimpleTask(group1);
testSimpleTask(group2);
testSimpleTask(group3);
// attempt to execute tasks that run with only frames from boot
// class loader on the stack.
testAttackingTask(group1);
testAttackingTask(group2);
testAttackingTask(group3);
} finally {
group1.shutdown();
group2.shutdown();
group3.shutdown();
}
}
public static void main(String[] args) throws Exception {
// create channel groups
ThreadFactory factory = new PrivilegedThreadFactory();
AsynchronousChannelGroup group1 = AsynchronousChannelGroup
.withFixedThreadPool(5, factory);
AsynchronousChannelGroup group2 = AsynchronousChannelGroup
.withCachedThreadPool(Executors.newCachedThreadPool(factory), 0);
AsynchronousChannelGroup group3 = AsynchronousChannelGroup
.withThreadPool(Executors.newFixedThreadPool(10, factory));
try {
// execute simple tasks
testSimpleTask(group1);
testSimpleTask(group2);
testSimpleTask(group3);
// install security manager and test again
System.setSecurityManager( new SecurityManager() );
testSimpleTask(group1);
testSimpleTask(group2);
testSimpleTask(group3);
// attempt to execute tasks that run with only frames from boot
// class loader on the stack.
testAttackingTask(group1);
testAttackingTask(group2);
testAttackingTask(group3);
} finally {
group1.shutdown();
group2.shutdown();
group3.shutdown();
}
}
public static void main(String[] args) throws Exception {
// create channel groups
ThreadFactory factory = new PrivilegedThreadFactory();
AsynchronousChannelGroup group1 = AsynchronousChannelGroup
.withFixedThreadPool(5, factory);
AsynchronousChannelGroup group2 = AsynchronousChannelGroup
.withCachedThreadPool(Executors.newCachedThreadPool(factory), 0);
AsynchronousChannelGroup group3 = AsynchronousChannelGroup
.withThreadPool(Executors.newFixedThreadPool(10, factory));
try {
// execute simple tasks
testSimpleTask(group1);
testSimpleTask(group2);
testSimpleTask(group3);
// install security manager and test again
System.setSecurityManager( new SecurityManager() );
testSimpleTask(group1);
testSimpleTask(group2);
testSimpleTask(group3);
// attempt to execute tasks that run with only frames from boot
// class loader on the stack.
testAttackingTask(group1);
testAttackingTask(group2);
testAttackingTask(group3);
} finally {
group1.shutdown();
group2.shutdown();
group3.shutdown();
}
}
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
AsynchronousChannelGroup channelGroup = AsynchronousChannelGroup.withFixedThreadPool(Runtime.getRuntime().availableProcessors(), new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
return new Thread(r);
}
});
StringProtocol protocol = new StringProtocol();
PushClientProcessorMessage clientProcessorMessage = new PushClientProcessorMessage();
AioQuickClient<String>[] clients = new AioQuickClient[4];
for (int i = 0; i < clients.length; i++) {
clients[i] = new AioQuickClient<>("localhost", 8080, protocol, clientProcessorMessage);
clients[i].start(channelGroup);
}
}
public static void main(String[] args) throws Exception {
// create channel groups
ThreadFactory factory = new PrivilegedThreadFactory();
AsynchronousChannelGroup group1 = AsynchronousChannelGroup
.withFixedThreadPool(5, factory);
AsynchronousChannelGroup group2 = AsynchronousChannelGroup
.withCachedThreadPool(Executors.newCachedThreadPool(factory), 0);
AsynchronousChannelGroup group3 = AsynchronousChannelGroup
.withThreadPool(Executors.newFixedThreadPool(10, factory));
try {
// execute simple tasks
testSimpleTask(group1);
testSimpleTask(group2);
testSimpleTask(group3);
// install security manager and test again
System.setSecurityManager( new SecurityManager() );
testSimpleTask(group1);
testSimpleTask(group2);
testSimpleTask(group3);
// attempt to execute tasks that run with only frames from boot
// class loader on the stack.
testAttackingTask(group1);
testAttackingTask(group2);
testAttackingTask(group3);
} finally {
group1.shutdown();
group2.shutdown();
group3.shutdown();
}
}
public static void main(String[] args) throws Exception {
// create channel groups
ThreadFactory factory = new PrivilegedThreadFactory();
AsynchronousChannelGroup group1 = AsynchronousChannelGroup
.withFixedThreadPool(5, factory);
AsynchronousChannelGroup group2 = AsynchronousChannelGroup
.withCachedThreadPool(Executors.newCachedThreadPool(factory), 0);
AsynchronousChannelGroup group3 = AsynchronousChannelGroup
.withThreadPool(Executors.newFixedThreadPool(10, factory));
try {
// execute simple tasks
testSimpleTask(group1);
testSimpleTask(group2);
testSimpleTask(group3);
// install security manager and test again
System.setSecurityManager( new SecurityManager() );
testSimpleTask(group1);
testSimpleTask(group2);
testSimpleTask(group3);
// attempt to execute tasks that run with only frames from boot
// class loader on the stack.
testAttackingTask(group1);
testAttackingTask(group2);
testAttackingTask(group3);
} finally {
group1.shutdown();
group2.shutdown();
group3.shutdown();
}
}
public static void main(String[] args) throws Exception {
// create channel groups
ThreadFactory factory = new PrivilegedThreadFactory();
AsynchronousChannelGroup group1 = AsynchronousChannelGroup
.withFixedThreadPool(5, factory);
AsynchronousChannelGroup group2 = AsynchronousChannelGroup
.withCachedThreadPool(Executors.newCachedThreadPool(factory), 0);
AsynchronousChannelGroup group3 = AsynchronousChannelGroup
.withThreadPool(Executors.newFixedThreadPool(10, factory));
try {
// execute simple tasks
testSimpleTask(group1);
testSimpleTask(group2);
testSimpleTask(group3);
// install security manager and test again
System.setSecurityManager( new SecurityManager() );
testSimpleTask(group1);
testSimpleTask(group2);
testSimpleTask(group3);
// attempt to execute tasks that run with only frames from boot
// class loader on the stack.
testAttackingTask(group1);
testAttackingTask(group2);
testAttackingTask(group3);
} finally {
group1.shutdown();
group2.shutdown();
group3.shutdown();
}
}
public static void main(String[] args) throws Exception {
// create channel groups
ThreadFactory factory = new PrivilegedThreadFactory();
AsynchronousChannelGroup group1 = AsynchronousChannelGroup
.withFixedThreadPool(5, factory);
AsynchronousChannelGroup group2 = AsynchronousChannelGroup
.withCachedThreadPool(Executors.newCachedThreadPool(factory), 0);
AsynchronousChannelGroup group3 = AsynchronousChannelGroup
.withThreadPool(Executors.newFixedThreadPool(10, factory));
try {
// execute simple tasks
testSimpleTask(group1);
testSimpleTask(group2);
testSimpleTask(group3);
// install security manager and test again
System.setSecurityManager( new SecurityManager() );
testSimpleTask(group1);
testSimpleTask(group2);
testSimpleTask(group3);
// attempt to execute tasks that run with only frames from boot
// class loader on the stack.
testAttackingTask(group1);
testAttackingTask(group2);
testAttackingTask(group3);
} finally {
group1.shutdown();
group2.shutdown();
group3.shutdown();
}
}
public void setUpHandlers() {
try {
AsynchronousChannelGroup asyncChannelGroup = AsynchronousChannelGroup.withFixedThreadPool(poolSize, Executors.defaultThreadFactory());
serverSocketChannel = AsynchronousServerSocketChannel.open(asyncChannelGroup).bind(new InetSocketAddress(listenPort));
serverSocketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
log.info("** " + poolSize + " handler thread has been setup! **");
log.info("** Socket Server has been startup, listen port is " + listenPort + "! **");
} catch (IOException e) {
log.error("setUpHandlers error: ", e);
}
}
public static void main(String[] args) throws Exception {
// create channel groups
ThreadFactory factory = new PrivilegedThreadFactory();
AsynchronousChannelGroup group1 = AsynchronousChannelGroup
.withFixedThreadPool(5, factory);
AsynchronousChannelGroup group2 = AsynchronousChannelGroup
.withCachedThreadPool(Executors.newCachedThreadPool(factory), 0);
AsynchronousChannelGroup group3 = AsynchronousChannelGroup
.withThreadPool(Executors.newFixedThreadPool(10, factory));
try {
// execute simple tasks
testSimpleTask(group1);
testSimpleTask(group2);
testSimpleTask(group3);
// install security manager and test again
System.setSecurityManager( new SecurityManager() );
testSimpleTask(group1);
testSimpleTask(group2);
testSimpleTask(group3);
// attempt to execute tasks that run with only frames from boot
// class loader on the stack.
testAttackingTask(group1);
testAttackingTask(group2);
testAttackingTask(group3);
} finally {
group1.shutdown();
group2.shutdown();
group3.shutdown();
}
}
public void setUpHandlers() {
try {
AsynchronousChannelGroup asyncChannelGroup = AsynchronousChannelGroup
.withFixedThreadPool(poolSize,Executors.defaultThreadFactory());
serverSocketChannel = AsynchronousServerSocketChannel
.open(asyncChannelGroup).bind(new InetSocketAddress(listenPort));
serverSocketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
//serverSocketChannel.setOption(StandardSocketOption.TCP_NODELAY, true);
} catch (IOException e) {
e.printStackTrace();
}
log.info("** " + poolSize + " handler thread has been setup! **");
log.info("** Socket Server has been startup, listen port is " + listenPort + "! **");
}
public static void main(String[] args) throws Exception {
// create channel groups
ThreadFactory factory = new PrivilegedThreadFactory();
AsynchronousChannelGroup group1 = AsynchronousChannelGroup
.withFixedThreadPool(5, factory);
AsynchronousChannelGroup group2 = AsynchronousChannelGroup
.withCachedThreadPool(Executors.newCachedThreadPool(factory), 0);
AsynchronousChannelGroup group3 = AsynchronousChannelGroup
.withThreadPool(Executors.newFixedThreadPool(10, factory));
try {
// execute simple tasks
testSimpleTask(group1);
testSimpleTask(group2);
testSimpleTask(group3);
// install security manager and test again
System.setSecurityManager( new SecurityManager() );
testSimpleTask(group1);
testSimpleTask(group2);
testSimpleTask(group3);
// attempt to execute tasks that run with only frames from boot
// class loader on the stack.
testAttackingTask(group1);
testAttackingTask(group2);
testAttackingTask(group3);
} finally {
group1.shutdown();
group2.shutdown();
group3.shutdown();
}
}
@BeforeTest
void setup() throws IOException {
GROUP = AsynchronousChannelGroup.withFixedThreadPool(2,
Executors.defaultThreadFactory());
}
@BeforeTest
void setup() throws IOException {
GROUP = AsynchronousChannelGroup.withFixedThreadPool(2,
Executors.defaultThreadFactory());
}
/**
* 内部启动逻辑
*
* @param aioSessionFunction 实例化会话的Function
* @throws IOException IO异常
*/
private final void start0(Function<AsynchronousSocketChannel, TcpAioSession<T>> aioSessionFunction) throws IOException {
checkAndResetConfig();
try {
aioWriteCompletionHandler = new WriteCompletionHandler<>();
if (bufferPool == null) {
this.bufferPool = config.getBufferFactory().create();
this.innerBufferPool = bufferPool;
}
this.aioSessionFunction = aioSessionFunction;
aioReadCompletionHandler = new ConcurrentReadCompletionHandler<>(new Semaphore(config.getThreadNum() - 1));
asynchronousChannelGroup = AsynchronousChannelGroup.withFixedThreadPool(config.getThreadNum(), new ThreadFactory() {
private byte index = 0;
@Override
public Thread newThread(Runnable r) {
return bufferPool.newThread(r, "smart-socket:Thread-" + (++index) + "-");
}
});
this.serverSocketChannel = AsynchronousServerSocketChannel.open(asynchronousChannelGroup);
//set socket options
if (config.getSocketOptions() != null) {
for (Map.Entry<SocketOption<Object>, Object> entry : config.getSocketOptions().entrySet()) {
this.serverSocketChannel.setOption(entry.getKey(), entry.getValue());
}
}
//bind host
if (config.getHost() != null) {
serverSocketChannel.bind(new InetSocketAddress(config.getHost(), config.getPort()), config.getBacklog());
} else {
serverSocketChannel.bind(new InetSocketAddress(config.getPort()), config.getBacklog());
}
startAcceptThread();
} catch (IOException e) {
shutdown();
throw e;
}
System.out.println("smart-socket server started on port " + config.getPort() + ",threadNum:" + config.getThreadNum());
System.out.println("smart-socket server config is " + config);
}
@BeforeTest
void setup() throws IOException {
GROUP = AsynchronousChannelGroup.withFixedThreadPool(2,
Executors.defaultThreadFactory());
}
/**
* 启动客户端。
*
* <p>
* 本方法会构建线程数为2的{@code asynchronousChannelGroup},并通过调用{@link AioQuickClient#start(AsynchronousChannelGroup)}启动服务。
* </p>
*
* @return 建立连接后的会话对象
* @throws IOException IOException
* @see AioQuickClient#start(AsynchronousChannelGroup)
*/
public final AioSession<T> start() throws IOException {
this.asynchronousChannelGroup = AsynchronousChannelGroup.withFixedThreadPool(2, new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
return new Thread(r);
}
});
return start(asynchronousChannelGroup);
}