本文整理匯總了Java中java.nio.channels.Selector.wakeup方法的典型用法代碼示例。如果您正苦於以下問題:Java Selector.wakeup方法的具體用法?Java Selector.wakeup怎麽用?Java Selector.wakeup使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.nio.channels.Selector
的用法示例。
在下文中一共展示了Selector.wakeup方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: stopListening
import java.nio.channels.Selector; //導入方法依賴的package包/類
/**
* Close Selector.
*
* @see org.apache.catalina.tribes.transport.ReceiverBase#stop()
*/
protected void stopListening() {
setListen(false);
Selector selector = this.selector.get();
if (selector != null) {
try {
// Unlock the thread if is is blocked waiting for input
selector.wakeup();
// Wait for the receiver thread to finish
int count = 0;
while (running && count < 50) {
Thread.sleep(100);
count ++;
}
if (running) {
log.warn(sm.getString("NioReceiver.stop.threadRunning"));
}
closeSelector();
} catch (Exception x) {
log.error("Unable to close cluster receiver selector.", x);
} finally {
this.selector.set(null);
}
}
}
示例2: registerSelector
import java.nio.channels.Selector; //導入方法依賴的package包/類
public static SelectionKey registerSelector(final Selector selector,
final SocketChannel channel, final int op)
throws CancelledKeyException, ClosedChannelException {
// Register the selector at the channel, so that it will be notified
// on the socket's events
synchronized (RegisterGate) {
// Wakeup the currently blocking reader/writer thread; we have
// locked the RegisterGate to prevent the awakened thread to block again
selector.wakeup();
// Lock the selector to prevent the waiting worker threads going
// into selector.select() which would block the selector.
synchronized (selector) {
return channel.register(selector, op, null);
}
}
}
示例3: 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();
}
示例4: 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();
}
}
示例5: stopListening
import java.nio.channels.Selector; //導入方法依賴的package包/類
/**
* Close Selector.
*
* @see org.apache.catalina.tribes.transport.ReceiverBase#stop()
*/
protected void stopListening() {
setListen(false);
Selector selector = this.selector.get();
if (selector != null) {
try {
// Unlock the thread if is is blocked waiting for input
selector.wakeup();
// Wait for the receiver thread to finish
int count = 0;
while (running && count < 50) {
Thread.sleep(100);
count++;
}
if (running) {
log.warn(sm.getString("NioReceiver.stop.threadRunning"));
}
closeSelector();
} catch (Exception x) {
log.error("Unable to close cluster receiver selector.", x);
} finally {
this.selector.set(null);
}
}
}
示例6: reply
import java.nio.channels.Selector; //導入方法依賴的package包/類
void reply(XulHttpServerResponse serverResponse) {
_response = serverResponse;
serverResponse.addHeaderIfNotExists("Content-Type", "text/html")
.addHeaderIfNotExists("Connection", "close");
final String transferEncoding = _response.headers.get("Transfer-Encoding");
_sendChunkedData = "chunked".equals(transferEncoding);
serverResponse.prepareResponseData();
_responseBuffer = ByteBuffer.wrap(serverResponse.getData(), 0, serverResponse.getDataSize());
try {
Selector selector = _server._selector;
_socketChannel.register(selector, SelectionKey.OP_WRITE, this);
selector.wakeup();
} catch (ClosedChannelException e) {
clear();
XulLog.e(TAG, e);
}
}
示例7: addEvent
import java.nio.channels.Selector; //導入方法依賴的package包/類
public void addEvent(Runnable event) {
Selector selector = this.selector.get();
if ( selector != null ) {
synchronized (events) {
events.add(event);
}
if ( log.isTraceEnabled() ) log.trace("Adding event to selector:"+event);
if ( isListening() ) selector.wakeup();
}
}
示例8: Handler
import java.nio.channels.Selector; //導入方法依賴的package包/類
public Handler(Selector sel, SocketChannel c) throws IOException {
socket = c;
c.configureBlocking(false);
// Optionally try first read now
sk = socket.register(sel, 0);
sk.attach(this); // 將Handler作為callback對象
sk.interestOps(SelectionKey.OP_READ); // 第二步,接收Read事件
sel.wakeup();
}
示例9: initialKeyRegister
import java.nio.channels.Selector; //導入方法依賴的package包/類
void initialKeyRegister(FDTSelectionKey fdtSelectionKey) {
final Selector sel = fdtSelectionKey.selector;
final SelectionTask st = fdtSelectionKey.selectionTask;
boolean bShouldWakeup = false;
// something gone wrong if either the Selector or the SelectionTask are
// null
// final ReentrantLock lock = st.lock;
// lock.lock();
// try {
// } finally {
// lock.unlock();
// }
synchronized (st) {
if (st.renewQueue.isEmpty() && st.newQueue.isEmpty()) {
bShouldWakeup = true;
}
st.newQueue.add(fdtSelectionKey);
}
if (bShouldWakeup) {
sel.wakeup();
}
}
示例10: 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();
}
示例11: addEvent
import java.nio.channels.Selector; //導入方法依賴的package包/類
public void addEvent(Runnable event) {
Selector selector = this.selector.get();
if (selector != null) {
synchronized (events) {
events.add(event);
}
if (log.isTraceEnabled())
log.trace("Adding event to selector:" + event);
if (isListening())
selector.wakeup();
}
}
示例12: notifyWritable
import java.nio.channels.Selector; //導入方法依賴的package包/類
void notifyWritable() throws IOException {
if (_responseBuffer == null) {
return;
}
final SocketChannel socketChannel = _socketChannel;
socketChannel.write(_responseBuffer);
if (!_responseBuffer.hasRemaining()) {
if (_response.hasUserBodyStream()) {
final Selector selector = _server._selector;
final XulHttpServerHandler attachment = this;
socketChannel.register(selector, 0, attachment);
selector.wakeup();
_server._reactorPool.execute(new Runnable() {
@Override
public void run() {
try {
int beginOffset = _sendChunkedData ? 32 : 0;
int endOffset = _sendChunkedData ? 2 : 0;
int sizeLimit = _sendChunkedData ? 8192 : -1;
if (_response == null || !_response.prepareUserBodyData(beginOffset, endOffset, sizeLimit)) {
terminate();
return;
}
int dataSize = _response.getDataSize();
if (dataSize <= 0) {
if (_sendChunkedData) {
_response.writeStream(null);
_responseBuffer = ByteBuffer.wrap("0\r\n\r\n".getBytes());
} else {
terminate();
return;
}
} else {
final byte[] data = _response.getData();
if (_sendChunkedData) {
String dataLength = String.format("%X\r\n", dataSize);
final byte[] dataLengthBytes = dataLength.getBytes();
beginOffset -= dataLengthBytes.length;
System.arraycopy(dataLengthBytes, 0, data, beginOffset, dataLengthBytes.length);
dataSize += dataLengthBytes.length;
data[beginOffset + dataSize++] = '\r';
data[beginOffset + dataSize++] = '\n';
}
_responseBuffer = ByteBuffer.wrap(data, beginOffset, dataSize);
}
socketChannel.register(selector, SelectionKey.OP_WRITE, attachment);
selector.wakeup();
} catch (Exception e) {
terminate();
XulLog.e(TAG, e);
}
}
});
} else {
socketChannel.close();
}
return;
}
}