FileDocCategorySizeDatePackage
SelectorFactory.javaAPI DocGlassfish v2 API4064Fri May 04 22:37:06 BST 2007com.sun.enterprise.web.connector.grizzly

SelectorFactory

public class SelectorFactory extends Object
Factory used to dispatch/share Selector.
author
Scott Oaks
author
Jean-Francois Arcand

Fields Summary
protected static long
timeout
The timeout before we exit.
protected static int
maxSelectors
The number of Selector to create.
private static final Stack
selectors
Cache of Selector
Constructors Summary
Methods Summary
public static final java.nio.channels.SelectorgetSelector()
Get a exclusive Selector

    
    
            
     
        try{
            for (int i = 0; i < maxSelectors; i++) 
                selectors.add(Selector.open());
        } catch (IOException ex){
            ; // do nothing.
        }
    
        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;
        }
    
public static final voidreturnSelector(java.nio.channels.Selector s)
Return the Selector to the cache

        synchronized(selectors) {
            selectors.push(s);
            if (selectors.size() == 1)
                selectors.notify();
        }