/**
* <strong>Selector缓存(线程相关)</strong>
* <p>
* 1. 减少线程切换时间
* 2. 如果在线程的每次run调用时初始化Selector, 会增加生成对象的开销
*
* </p>
*
* @author: 汉娱网络.技术部.yangxf
* @date: 2007-12-27
* @version: 1.1
**/
package com.handjoys.socket;
import java.io.IOException;
import java.nio.channels.Selector;
import java.util.EmptyStackException;
import java.util.Stack;
public class SelectorFactory{
protected static long timeout = 5000;
protected static int maxSelectors = 20;
/**
* Cache of <code>Selector</code>
*/
private final static Stack<Selector> selectors = new Stack<Selector>();
/**
* Creates the <code>Selector</code>
*/
static {
try{
for (int i = 0; i < maxSelectors; i++)
selectors.add(Selector.open());
} catch (IOException ex){
; // do nothing.
}
}
/**
* Get a exclusive <code>Selector</code>
*/
public final static 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;
}
}
/**
* Return the <code>Selector</code> to the cache
*/
public final static void returnSelector(Selector s) {
synchronized(selectors) {
selectors.push(s);
if (selectors.size() == 1)
selectors.notify();
}
}
}