public NetAcceptor(int port) throws IOException { AsynchronousChannelGroup group = AsynchronousChannelGroup.withCachedThreadPool( Executors.newCachedThreadPool(), 1); // server连接接收线程池 serverChannel = AsynchronousServerSocketChannel.open(group); serverChannel.bind(new InetSocketAddress(port)); }
private static AsynchronousChannelGroup createAsynchronousChannelGroup() { // Need to do this with the right thread context class loader else the // first web app to call this will trigger a leak ClassLoader original = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(AsyncIOThreadFactory.class.getClassLoader()); // These are the same settings as the default // AsynchronousChannelGroup int initialSize = Runtime.getRuntime().availableProcessors(); ExecutorService executorService = new ThreadPoolExecutor( 0, Integer.MAX_VALUE, Long.MAX_VALUE, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(), new AsyncIOThreadFactory()); try { return AsynchronousChannelGroup.withCachedThreadPool(executorService, initialSize); } catch (IOException e) { // No good reason for this to happen. throw new IllegalStateException(sm.getString("asyncChannelGroup.createFail")); } } finally { Thread.currentThread().setContextClassLoader(original); } }
public AIOSocketMgr() throws Exception { if (!allowInstance) { throw new Exception(); } try { AsynchronousChannelGroup resourceGroup = AsynchronousChannelGroup.withCachedThreadPool(Executors.newCachedThreadPool(), 8); server = AsynchronousServerSocketChannel.open(resourceGroup); server.bind(new InetSocketAddress(PORT), 100); acceptHandler = new AcceptCompletionHandler(); readHandler = new ReadCompletionHandler(); authHandler = new AuthSessionCompletionHandler(); bindProtocol(); } catch (IOException e) { e.printStackTrace(); } }