本文整理匯總了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);
}
}
示例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);
}
}
示例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();
}
示例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);
}
}
示例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);
}
}
示例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);
}
示例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];
}
}
}
示例8: register
import java.nio.channels.AsynchronousChannelGroup; //導入依賴的package包/類
public static AsynchronousChannelGroup register() {
synchronized (lock) {
if (usageCount == 0) {
group = createAsynchronousChannelGroup();
}
usageCount++;
return group;
}
}
示例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);
}
}
示例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;
}
示例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;
}
}
示例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;
}
示例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();
}
}
示例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();
}
示例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");
}