本文整理匯總了Java中java.nio.channels.SocketChannel.isBlocking方法的典型用法代碼示例。如果您正苦於以下問題:Java SocketChannel.isBlocking方法的具體用法?Java SocketChannel.isBlocking怎麽用?Java SocketChannel.isBlocking使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.nio.channels.SocketChannel
的用法示例。
在下文中一共展示了SocketChannel.isBlocking方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: connect
import java.nio.channels.SocketChannel; //導入方法依賴的package包/類
/**
* The contract is similar to {@link SocketChannel#connect(SocketAddress)}
* with a timeout.
*
* @see SocketChannel#connect(SocketAddress)
*
* @param channel - this should be a {@link SelectableChannel}
* @param endpoint
* @throws IOException
*/
static void connect(SocketChannel channel,
SocketAddress endpoint, int timeout) throws IOException {
boolean blockingOn = channel.isBlocking();
if (blockingOn) {
channel.configureBlocking(false);
}
try {
if (channel.connect(endpoint)) {
return;
}
long timeoutLeft = timeout;
long endTime = (timeout > 0) ? (Time.now() + timeout): 0;
while (true) {
// we might have to call finishConnect() more than once
// for some channels (with user level protocols)
int ret = selector.select((SelectableChannel)channel,
SelectionKey.OP_CONNECT, timeoutLeft);
if (ret > 0 && channel.finishConnect()) {
return;
}
if (ret == 0 ||
(timeout > 0 &&
(timeoutLeft = (endTime - Time.now())) <= 0)) {
throw new SocketTimeoutException(
timeoutExceptionString(channel, timeout,
SelectionKey.OP_CONNECT));
}
}
} catch (IOException e) {
// javadoc for SocketChannel.connect() says channel should be closed.
try {
channel.close();
} catch (IOException ignored) {}
throw e;
} finally {
if (blockingOn && channel.isOpen()) {
channel.configureBlocking(true);
}
}
}
示例2: connect
import java.nio.channels.SocketChannel; //導入方法依賴的package包/類
/**
* The contract is similar to {@link SocketChannel#connect(SocketAddress)}
* with a timeout.
*
* @see SocketChannel#connect(SocketAddress)
*
* @param channel - this should be a {@link SelectableChannel}
* @param endpoint
* @throws IOException
*/
static void connect(SocketChannel channel,
SocketAddress endpoint, int timeout) throws IOException {
boolean blockingOn = channel.isBlocking();
if (blockingOn) {
channel.configureBlocking(false);
}
try {
if (channel.connect(endpoint)) {
return;
}
long timeoutLeft = timeout;
long endTime = (timeout > 0) ? (System.currentTimeMillis() + timeout): 0;
while (true) {
// we might have to call finishConnect() more than once
// for some channels (with user level protocols)
int ret = selector.select((SelectableChannel)channel,
SelectionKey.OP_CONNECT, timeoutLeft);
if (ret > 0 && channel.finishConnect()) {
return;
}
if (ret == 0 ||
(timeout > 0 &&
(timeoutLeft = (endTime - System.currentTimeMillis())) <= 0)) {
throw new SocketTimeoutException(
timeoutExceptionString(channel, timeout,
SelectionKey.OP_CONNECT));
}
}
} catch (IOException e) {
// javadoc for SocketChannel.connect() says channel should be closed.
/*try {
channel.close();
} catch (IOException ignored) {}*/
throw e;
} finally {
if (blockingOn && channel.isOpen()) {
channel.configureBlocking(true);
}
}
}