Java Code Examples for java.nio.channels.AsynchronousChannelGroup#withFixedThreadPool()

The following examples show how to use java.nio.channels.AsynchronousChannelGroup#withFixedThreadPool() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AioServerImpl.java    From tephra with MIT License 6 votes vote down vote up
@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);
    }
}
 
Example 2
Source File: DbleServer.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
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;
                    }
                }
        );
    }
}
 
Example 3
Source File: AsExecutor.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 4
Source File: AsExecutor.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 5
Source File: AioSocketServer.java    From Tatala-RPC with Apache License 2.0 5 votes vote down vote up
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 + "! **");
}
 
Example 6
Source File: AsExecutor.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 7
Source File: AioSocketServer.java    From Tatala-RPC with Apache License 2.0 5 votes vote down vote up
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);
	}
}
 
Example 8
Source File: AsExecutor.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 9
Source File: AsExecutor.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 10
Source File: AsExecutor.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 11
Source File: ReceiverClient.java    From smart-socket with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 12
Source File: AsExecutor.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 13
Source File: AsExecutor.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 14
Source File: AsExecutor.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 15
Source File: AioSocketServer.java    From Tatala-RPC with Apache License 2.0 5 votes vote down vote up
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 + "! **");
}
 
Example 16
Source File: CompletionHandlerRelease.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@BeforeTest
void setup() throws IOException {
    GROUP = AsynchronousChannelGroup.withFixedThreadPool(2,
        Executors.defaultThreadFactory());
}
 
Example 17
Source File: AioQuickServer.java    From smart-socket with Apache License 2.0 4 votes vote down vote up
/**
 * 内部启动逻辑
 *
 * @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);
}
 
Example 18
Source File: CompletionHandlerRelease.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@BeforeTest
void setup() throws IOException {
    GROUP = AsynchronousChannelGroup.withFixedThreadPool(2,
        Executors.defaultThreadFactory());
}
 
Example 19
Source File: CompletionHandlerRelease.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
@BeforeTest
void setup() throws IOException {
    GROUP = AsynchronousChannelGroup.withFixedThreadPool(2,
        Executors.defaultThreadFactory());
}
 
Example 20
Source File: AioQuickClient.java    From smart-socket with Apache License 2.0 3 votes vote down vote up
/**
 * 启动客户端。
 *
 * <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);
}