當前位置: 首頁>>代碼示例>>Java>>正文


Java AsynchronousChannelGroup類代碼示例

本文整理匯總了Java中java.nio.channels.AsynchronousChannelGroup的典型用法代碼示例。如果您正苦於以下問題:Java AsynchronousChannelGroup類的具體用法?Java AsynchronousChannelGroup怎麽用?Java AsynchronousChannelGroup使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


AsynchronousChannelGroup類屬於java.nio.channels包,在下文中一共展示了AsynchronousChannelGroup類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: createAsynchronousChannelGroup

import java.nio.channels.AsynchronousChannelGroup; //導入依賴的package包/類
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);
	}
}
 
開發者ID:how2j,項目名稱:lazycat,代碼行數:25,代碼來源:AsyncChannelGroupUtil.java

示例2: listen

import java.nio.channels.AsynchronousChannelGroup; //導入依賴的package包/類
@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);
    }
}
 
開發者ID:heisedebaise,項目名稱:tephra,代碼行數:18,代碼來源:AioServerImpl.java

示例3: startServer

import java.nio.channels.AsynchronousChannelGroup; //導入依賴的package包/類
public synchronized void startServer(SocketAddress addr, Object attachment, AsynchronousChannelGroup group)
{
	stopServer();
	try
	{
		_acceptor = AsynchronousServerSocketChannel.open(group);
		int backlog = onAcceptorCreated(_acceptor, attachment);
		if(backlog >= 0)
		{
			_acceptor.bind(addr, backlog);
			beginAccept();
			return;
		}
	}
	catch(Throwable e)
	{
		doException(null, e);
	}
	stopServer();
}
 
開發者ID:dwing4g,項目名稱:jane,代碼行數:21,代碼來源:TcpManager.java

示例4: startClient

import java.nio.channels.AsynchronousChannelGroup; //導入依賴的package包/類
@SuppressWarnings("resource")
public void startClient(SocketAddress addr, Object attachment, AsynchronousChannelGroup group)
{
	AsynchronousSocketChannel channel = null;
	try
	{
		channel = AsynchronousSocketChannel.open(group);
		int recvBufSize = onChannelCreated(channel, attachment);
		if(recvBufSize >= 0)
			channel.connect(addr, new ConnectParam(channel, recvBufSize), _connectHandler);
		else
			channel.close();
	}
	catch(Throwable e)
	{
		doException(null, e);
		closeChannel(channel);
	}
}
 
開發者ID:dwing4g,項目名稱:jane,代碼行數:20,代碼來源:TcpManager.java

示例5: run

import java.nio.channels.AsynchronousChannelGroup; //導入依賴的package包/類
@Override
public void run() {
    try {
        final int workerThreads = Math.max(4, 2 *
                Runtime.getRuntime().availableProcessors());
        channelGroup = AsynchronousChannelGroup.withFixedThreadPool(
                workerThreads, Executors.defaultThreadFactory());

        serverSocketChannel = AsynchronousServerSocketChannel.open(channelGroup);
        serverSocketChannel.bind(new InetSocketAddress(port));

        serverSocketChannel.accept(null,
                new AcceptCompletionHandler(serverSocketChannel));

        channelGroup.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
    } catch(IOException | InterruptedException ex) {
        Log.get().log(Level.SEVERE, ex.getLocalizedMessage(), ex);
    }
}
 
開發者ID:cli,項目名稱:sonews,代碼行數:20,代碼來源:AsynchronousNNTPDaemon.java

示例6: AIOAcceptor

import java.nio.channels.AsynchronousChannelGroup; //導入依賴的package包/類
public AIOAcceptor(String name, String ip, int port,
		FrontendConnectionFactory factory, AsynchronousChannelGroup group)
		throws IOException {
	this.name = name;
	this.port = port;
	this.factory = factory;
	serverChannel = AsynchronousServerSocketChannel.open(group);
	/** 設置TCP屬性 */
	serverChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
	serverChannel.setOption(StandardSocketOptions.SO_RCVBUF, 1024 * 16 * 2);
	// backlog=100
	serverChannel.bind(new InetSocketAddress(ip, port), 100);
}
 
開發者ID:huang-up,項目名稱:mycat-src-1.6.1-RELEASE,代碼行數:14,代碼來源:AIOAcceptor.java

示例7: getNextAsyncChannelGroup

import java.nio.channels.AsynchronousChannelGroup; //導入依賴的package包/類
/**
 * get next AsynchronousChannel ,first is exclude if multi
 * AsynchronousChannelGroups
 *
 * @return
 */
public AsynchronousChannelGroup getNextAsyncChannelGroup() {
	if (asyncChannelGroups.length == 1) {
		return asyncChannelGroups[0];
	} else {
		int index = (++channelIndex) % asyncChannelGroups.length;
		if (index == 0) {
			++channelIndex;
			return asyncChannelGroups[1];
		} else {
			return asyncChannelGroups[index];
		}

	}
}
 
開發者ID:huang-up,項目名稱:mycat-src-1.6.1-RELEASE,代碼行數:21,代碼來源:MycatServer.java

示例8: register

import java.nio.channels.AsynchronousChannelGroup; //導入依賴的package包/類
public static AsynchronousChannelGroup register() {
    synchronized (lock) {
        if (usageCount == 0) {
            group = createAsynchronousChannelGroup();
        }
        usageCount++;
        return group;
    }
}
 
開發者ID:liaokailin,項目名稱:tomcat7,代碼行數:10,代碼來源:AsyncChannelGroupUtil.java

示例9: createAsynchronousChannelGroup

import java.nio.channels.AsynchronousChannelGroup; //導入依賴的package包/類
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);
    }
}
 
開發者ID:liaokailin,項目名稱:tomcat7,代碼行數:31,代碼來源:AsyncChannelGroupUtil.java

示例10: getAsynchronousChannelGroup

import java.nio.channels.AsynchronousChannelGroup; //導入依賴的package包/類
private AsynchronousChannelGroup getAsynchronousChannelGroup() {
    // Use AsyncChannelGroupUtil to share a common group amongst all
    // WebSocket clients
    AsynchronousChannelGroup result = asynchronousChannelGroup;
    if (result == null) {
        synchronized (asynchronousChannelGroupLock) {
            if (asynchronousChannelGroup == null) {
                asynchronousChannelGroup = AsyncChannelGroupUtil.register();
            }
            result = asynchronousChannelGroup;
        }
    }
    return result;
}
 
開發者ID:liaokailin,項目名稱:tomcat7,代碼行數:15,代碼來源:WsWebSocketContainer.java

示例11: DefaultThreadPool

import java.nio.channels.AsynchronousChannelGroup; //導入依賴的package包/類
private DefaultThreadPool(final int numberOfThreads, final ThreadPoolId id) {
    this.id = id;

    if (ThreadPoolId.WorkStealing == id) {
        this.executorService = new ForkJoinPool(
                numberOfThreads                                    /* parallelism level */,
                ForkJoinPool.defaultForkJoinWorkerThreadFactory,
                EhSupport.getUncaughtHandler()                     /* uncaught exception handler */,
                true                                    /* asyncMode */
        );
    } else {
        this.executorService = Executors.newFixedThreadPool(
                numberOfThreads,
                (runnable) -> defaultFactory.newThread(() -> EhSupport.fatalOnException(() -> {
                    Thread.currentThread().setUncaughtExceptionHandler(EhSupport.getUncaughtHandler());
                    runnable.run();
                }))
        );
    }

    if (ThreadPoolId.NonBlocking == this.id) {
        try {
            this.ioService = EhSupport.propagateFn(
                    () -> AsynchronousChannelGroup.withThreadPool(this.executorService)
            );
        } catch (final Throwable throwable) {
            this.executorService.shutdownNow();
            throw throwable;
        }
    } else {
        this.ioService = null;
    }
}
 
開發者ID:jpmorganchase,項目名稱:swblocks-jbl,代碼行數:34,代碼來源:DefaultThreadPool.java

示例12: ioService

import java.nio.channels.AsynchronousChannelGroup; //導入依賴的package包/類
@Override
public AsynchronousChannelGroup ioService() {
    EhSupport.ensureOrFatal(
            null != this.ioService,
            "I/O service requested for thread pool which does not support it"
    );

    return this.ioService;
}
 
開發者ID:jpmorganchase,項目名稱:swblocks-jbl,代碼行數:10,代碼來源:DefaultThreadPool.java

示例13: main

import java.nio.channels.AsynchronousChannelGroup; //導入依賴的package包/類
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();
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:34,代碼來源:AsExecutor.java

示例14: testSimpleTask

import java.nio.channels.AsynchronousChannelGroup; //導入依賴的package包/類
static void testSimpleTask(AsynchronousChannelGroup group) throws Exception {
    Executor executor = (Executor)group;
    final CountDownLatch latch = new CountDownLatch(1);
    executor.execute(new Runnable() {
        public void run() {
            latch.countDown();
        }
    });
    latch.await();
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:11,代碼來源:AsExecutor.java

示例15: testAttackingTask

import java.nio.channels.AsynchronousChannelGroup; //導入依賴的package包/類
static void testAttackingTask(AsynchronousChannelGroup group) throws Exception {
    Executor executor = (Executor)group;
    Attack task = new Attack();
    executor.execute(task);
    task.waitUntilDone();
    if (!task.failedDueToSecurityException())
        throw new RuntimeException("SecurityException expected");
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:9,代碼來源:AsExecutor.java


注:本文中的java.nio.channels.AsynchronousChannelGroup類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。