1 23 package com.sun.enterprise.web.connector.grizzly; 24 25 import java.io.IOException ; 26 import java.nio.channels.Selector ; 27 import java.util.EmptyStackException ; 28 import java.util.Stack ; 29 30 36 public class SelectorFactory{ 37 38 41 protected static long timeout = 5000; 42 43 44 47 protected static int maxSelectors = 20; 48 49 50 53 private final static Stack <Selector > selectors = new Stack <Selector >(); 54 55 56 59 static { 60 try{ 61 for (int i = 0; i < maxSelectors; i++) 62 selectors.add(Selector.open()); 63 } catch (IOException ex){ 64 ; } 66 } 67 68 69 72 public final static Selector getSelector() { 73 synchronized(selectors) { 74 Selector s = null; 75 try { 76 if ( selectors.size() != 0 ) 77 s = selectors.pop(); 78 } catch (EmptyStackException ex){} 79 80 int attempts = 0; 81 try{ 82 while (s == null && attempts < 2) { 83 selectors.wait(timeout); 84 try { 85 if ( selectors.size() != 0 ) 86 s = selectors.pop(); 87 } catch (EmptyStackException ex){ 88 break; 89 } 90 attempts++; 91 } 92 } catch (InterruptedException ex){}; 93 return s; 94 } 95 } 96 97 98 101 public final static void returnSelector(Selector s) { 102 synchronized(selectors) { 103 selectors.push(s); 104 if (selectors.size() == 1) 105 selectors.notify(); 106 } 107 } 108 109 } 110 | Popular Tags |