use of java.nio.channels.Selector in project hs4j by killme2008.
the class SelectorFactory method reduce.
/**
* Decrease <code>Selector</code> pool size
*/
private static void reduce(int size) {
for (int i = 0; i < maxSelectors - size; i++) {
try {
Selector selector = selectors.pop();
selector.close();
} catch (IOException e) {
logger.error("SelectorFactory.reduce", e);
}
}
}
use of java.nio.channels.Selector in project hs4j by killme2008.
the class SelectorFactory method getSelector.
/**
* Get a exclusive <code>Selector</code>
*
* @return <code>Selector</code>
*/
public static final Selector getSelector() {
synchronized (selectors) {
Selector s = null;
try {
if (selectors.size() != 0) {
s = selectors.pop();
}
} catch (EmptyStackException ex) {
}
int attempts = 0;
try {
while (s == null && attempts < 2) {
selectors.wait(timeout);
try {
if (selectors.size() != 0) {
s = selectors.pop();
}
} catch (EmptyStackException ex) {
break;
}
attempts++;
}
} catch (InterruptedException ex) {
}
return s;
}
}
use of java.nio.channels.Selector in project blade by biezhi.
the class ManagedSelector method dump.
@Override
public void dump(Appendable out, String indent) throws IOException {
out.append(String.valueOf(this)).append(" id=").append(String.valueOf(_id)).append(System.lineSeparator());
Selector selector = _selector;
if (selector != null && selector.isOpen()) {
final ArrayList<Object> dump = new ArrayList<>(selector.keys().size() * 2);
DumpKeys dumpKeys = new DumpKeys(dump);
submit(dumpKeys);
dumpKeys.await(5, TimeUnit.SECONDS);
ContainerLifeCycle.dump(out, indent, dump);
}
}
use of java.nio.channels.Selector in project blade by biezhi.
the class ManagedSelector method submit.
public void submit(Runnable change) {
if (LOG.isDebugEnabled())
LOG.debug("Queued change {} on {}", change, this);
Selector selector = null;
try (Locker.Lock lock = _locker.lock()) {
_actions.offer(change);
if (_selecting) {
selector = _selector;
// To avoid the extra select wakeup.
_selecting = false;
}
}
if (selector != null)
selector.wakeup();
}
use of java.nio.channels.Selector in project http-kit by http-kit.
the class PerformanceBench method main.
public static void main(String[] args) throws IOException, InterruptedException {
System.out.println("concurrency: " + concurrency + "; total requests: " + total);
Selector selector = Selector.open();
for (int i = 0; i < concurrency; ++i) {
if (i % 100 == 0) {
Thread.sleep(10);
}
connect(addr, selector);
}
start = System.currentTimeMillis();
while (true) {
int select = selector.select();
D("selec return " + select);
if (select > 0) {
Set<SelectionKey> selectedKeys = selector.selectedKeys();
Iterator<SelectionKey> it = selectedKeys.iterator();
while (it.hasNext()) {
SelectionKey key = it.next();
if (key.isConnectable()) {
SocketChannel ch = (SocketChannel) key.channel();
try {
if (ch.finishConnect()) {
key.interestOps(SelectionKey.OP_WRITE);
}
} catch (Exception e) {
ch.close();
e.printStackTrace();
D(e.getMessage() + "\t" + "close and reconnect");
// reconnect
connect(addr, selector);
}
} else if (key.isWritable()) {
doWrite(key);
} else if (key.isReadable()) {
doRead(selector, key);
}
}
selectedKeys.clear();
}
}
}
Aggregations