本文整理汇总了Java中java.nio.channels.Selector.close方法的典型用法代码示例。如果您正苦于以下问题:Java Selector.close方法的具体用法?Java Selector.close怎么用?Java Selector.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.Selector
的用法示例。
在下文中一共展示了Selector.close方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import java.nio.channels.Selector; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
final Selector sel = Selector.open();
Runnable r = new Runnable() {
public void run() {
try {
sel.select();
} catch (IOException x) {
x.printStackTrace();
}
}
};
// start thread to block in Selector
Thread t = new Thread(r);
t.start();
// give thread time to start
Thread.sleep(1000);
// interrupt, close, and wakeup is the magic sequence to provoke the NPE
t.interrupt();
sel.close();
sel.wakeup();
}
示例2: main
import java.nio.channels.Selector; //导入方法依赖的package包/类
public static void main(String argv[]) throws Exception {
int waitTime = 4000;
Selector selector = Selector.open();
try {
selector.wakeup();
long t1 = System.currentTimeMillis();
selector.select(waitTime);
long t2 = System.currentTimeMillis();
long totalTime = t2 - t1;
if (totalTime > waitTime)
throw new RuntimeException("Test failed");
} finally {
selector.close();
}
}
示例3: put
import java.nio.channels.Selector; //导入方法依赖的package包/类
public void put(Selector s) throws IOException {
if ( SHARED ) return;
if ( enabled ) active.decrementAndGet();
if ( enabled && (maxSpareSelectors==-1 || spare.get() < Math.min(maxSpareSelectors,maxSelectors)) ) {
spare.incrementAndGet();
selectors.offer(s);
}
else s.close();
}
示例4: close
import java.nio.channels.Selector; //导入方法依赖的package包/类
public void close() throws IOException {
enabled = false;
Selector s;
while ( (s = selectors.poll()) != null ) s.close();
spare.set(0);
active.set(0);
if (blockingSelector!=null) {
blockingSelector.close();
}
if ( SHARED && getSharedSelector()!=null ) {
getSharedSelector().close();
SHARED_SELECTOR = null;
}
}
示例5: main
import java.nio.channels.Selector; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
final Selector sel = Selector.open();
Runnable r = new Runnable() {
public void run() {
try {
sel.select();
} catch (IOException x) {
x.printStackTrace();
} catch (ClosedSelectorException y) {
System.err.println
("Caught expected ClosedSelectorException");
}
}
};
// start thread to block in Selector
Thread t = new Thread(r);
t.start();
// give thread time to start
Thread.sleep(1000);
// interrupt, close, and wakeup is the magic sequence to provoke the NPE
t.interrupt();
sel.close();
sel.wakeup();
}
示例6: put
import java.nio.channels.Selector; //导入方法依赖的package包/类
public void put(Selector s) throws IOException {
if (SHARED)
return;
if (enabled)
active.decrementAndGet();
if (enabled && (maxSpareSelectors == -1 || spare.get() < Math.min(maxSpareSelectors, maxSelectors))) {
spare.incrementAndGet();
selectors.offer(s);
} else
s.close();
}
示例7: close
import java.nio.channels.Selector; //导入方法依赖的package包/类
public void close() throws IOException {
enabled = false;
Selector s;
while ((s = selectors.poll()) != null)
s.close();
spare.set(0);
active.set(0);
if (blockingSelector != null) {
blockingSelector.close();
}
if (SHARED && getSharedSelector() != null) {
getSharedSelector().close();
SHARED_SELECTOR = null;
}
}
示例8: main
import java.nio.channels.Selector; //导入方法依赖的package包/类
public static void main(String args[]) throws Exception {
String msg = "HELLO";
// Launch the service with arguments to tell it to close
// the connection after reading 5 bytes ("HELLO"). After
// closing the connection the service should hang around
// for 15 seconds.
String service_args[] = new String[2];
service_args[0] = String.valueOf(msg.length());
service_args[1] = String.valueOf( 15*1000 );
SocketChannel sc = Launcher.launchWithSocketChannel("EchoService", service_args);
// send message - service will echo the message and close the connection.
sc.write(ByteBuffer.wrap(msg.getBytes("UTF-8")));
// read the reply (with timeout)
ByteBuffer bb = ByteBuffer.allocateDirect(50);
sc.configureBlocking(false);
Selector sel = sc.provider().openSelector();
SelectionKey sk = sc.register(sel, SelectionKey.OP_READ);
long to = 12 * 1000;
for (;;) {
long st = System.currentTimeMillis();
sel.select(to);
if (sk.isReadable()) {
int n = sc.read(bb);
// EOF
if (n < 0) {
break;
}
}
sel.selectedKeys().remove(sk);
to -= System.currentTimeMillis() - st;
if (to <= 0) {
throw new RuntimeException("Timed out waiting for connection to close");
}
}
sel.close();
sc.close();
// finally check that the reply length is okay
bb.flip();
if (bb.remaining() < msg.length()) {
throw new RuntimeException("Premature EOF from echo service");
}
System.out.println("Test passed - service closed connection.");
}
示例9: test
import java.nio.channels.Selector; //导入方法依赖的package包/类
private static boolean test(final long timeout)
throws InterruptedException, IOException {
AtomicReference<Exception> theException =
new AtomicReference<>();
AtomicBoolean isTimedOut = new AtomicBoolean();
Selector selector = Selector.open();
Thread t = new Thread(() -> {
try {
selector.select(timeout);
isTimedOut.set(true);
} catch (IOException ioe) {
theException.set(ioe);
}
});
t.start();
t.join(SLEEP_MILLIS);
boolean result;
if (theException.get() == null) {
if (timeout > SLEEP_MILLIS && isTimedOut.get()) {
System.err.printf("Test timed out early with timeout %d%n",
timeout);
result = false;
} else {
System.out.printf("Test succeeded with timeout %d%n", timeout);
result = true;
}
} else {
System.err.printf("Test failed with timeout %d%n", timeout);
theException.get().printStackTrace();
result = false;
}
t.interrupt();
selector.close();
return result;
}
示例10: closeQuietly
import java.nio.channels.Selector; //导入方法依赖的package包/类
/**
* Closes a <code>Selector</code> unconditionally.
* <p>
* Equivalent to {@link Selector#close()}, except any exceptions will be ignored. This is typically used in finally
* blocks.
* <p>
* Example code:
* <p>
* <pre>
* Selector selector = null;
* try {
* selector = Selector.open();
* // process socket
*
* } catch (Exception e) {
* // error handling
* } finally {
* IOUtils.closeQuietly(selector);
* }
* </pre>
*
* @param selector the Selector to close, may be null or already closed
* @since 2.2
*/
public static void closeQuietly(final Selector selector) {
if (selector != null) {
try {
selector.close();
} catch (final IOException ioe) {
logger.error("error close io", ioe);
}
}
}
示例11: closeQuietly
import java.nio.channels.Selector; //导入方法依赖的package包/类
/**
* Unconditionally close a <code>Selector</code>.
* <p/>
* Equivalent to {@link java.nio.channels.Selector#close()}, except any exceptions will be ignored.
* This is typically used in finally blocks.
* <p/>
* Example code:
* <pre>
* Selector selector = null;
* try {
* selector = Selector.open();
* // process socket
*
* } catch (Exception e) {
* // error handling
* } finally {
* IOUtils.closeQuietly(selector);
* }
* </pre>
*
* @param selector the Selector to close, may be null or already closed
* @since 2.2
*/
public static void closeQuietly(Selector selector) {
if (selector != null) {
try {
selector.close();
} catch (IOException ioe) {
// ignored
}
}
}
示例12: closeQuietly
import java.nio.channels.Selector; //导入方法依赖的package包/类
/**
* Unconditionally close a <code>Selector</code>.
* <p/>
* Equivalent to {@link Selector#close()}, except any exceptions will be ignored.
* This is typically used in finally blocks.
* <p/>
* Example code:
* <pre>
* Selector selector = null;
* try {
* selector = Selector.open();
* // process socket
*
* } catch (Exception e) {
* // error handling
* } finally {
* IOUtils.closeQuietly(selector);
* }
* </pre>
*
* @param selector the Selector to close, may be null or already closed
* @since 2.2
*/
public static void closeQuietly(Selector selector) {
if (selector != null) {
try {
selector.close();
} catch (IOException ioe) {
// ignored
}
}
}
示例13: closeQuietly
import java.nio.channels.Selector; //导入方法依赖的package包/类
/**
* Unconditionally close a <code>Selector</code>.
* <p>
* Equivalent to {@link Selector#close()}, except any exceptions will be ignored.
* This is typically used in finally blocks.
* <p>
* Example code:
* <pre>
* Selector selector = null;
* try {
* selector = Selector.open();
* // process socket
*
* } catch (Exception e) {
* // error handling
* } finally {
* IOUtils.closeQuietly(selector);
* }
* </pre>
*
* @param selector the Selector to close, may be null or already closed
* @since 2.2
*/
public static void closeQuietly(Selector selector){
if (selector != null){
try {
selector.close();
} catch (IOException ioe) {
// ignored
}
}
}