jdk/src/share/classes/sun/net/httpserver/SelectorCache.java
changeset 7338 567d424fccea
parent 7337 89ced394b320
parent 7325 3c9670ddb2a8
child 7339 0ce0a2c3a692
equal deleted inserted replaced
7337:89ced394b320 7338:567d424fccea
     1 /*
       
     2  * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package sun.net.httpserver;
       
    27 
       
    28 import java.util.*;
       
    29 import java.nio.*;
       
    30 import java.net.*;
       
    31 import java.io.*;
       
    32 import java.security.*;
       
    33 import java.nio.channels.*;
       
    34 
       
    35 /*
       
    36  * Implements a cache of java.nio.channels.Selector
       
    37  * where Selectors are allocated on demand and placed
       
    38  * in a temporary cache for a period of time, so they
       
    39  * can be reused. If a period of between 2 and 4 minutes
       
    40  * elapses without being used, then they are closed.
       
    41  */
       
    42 public class SelectorCache {
       
    43 
       
    44     static SelectorCache cache = null;
       
    45 
       
    46     private SelectorCache () {
       
    47         freeSelectors = new LinkedList<SelectorWrapper>();
       
    48         CacheCleaner c = AccessController.doPrivileged(
       
    49             new PrivilegedAction<CacheCleaner>() {
       
    50             public CacheCleaner run() {
       
    51                 CacheCleaner cleaner = new CacheCleaner();
       
    52                 cleaner.setDaemon (true);
       
    53                 return cleaner;
       
    54             }
       
    55         });
       
    56         c.start();
       
    57     }
       
    58 
       
    59     /**
       
    60      * factory method for creating single instance
       
    61      */
       
    62     public static SelectorCache getSelectorCache () {
       
    63         synchronized (SelectorCache.class) {
       
    64             if (cache == null) {
       
    65                 cache = new SelectorCache ();
       
    66             }
       
    67         }
       
    68         return cache;
       
    69     }
       
    70 
       
    71     private static class SelectorWrapper {
       
    72         private Selector sel;
       
    73         private boolean deleteFlag;
       
    74         private SelectorWrapper (Selector sel) {
       
    75             this.sel = sel;
       
    76             this.deleteFlag = false;
       
    77         }
       
    78         public Selector getSelector() { return sel;}
       
    79         public boolean getDeleteFlag () {return deleteFlag;}
       
    80         public void setDeleteFlag (boolean b) {deleteFlag = b;}
       
    81     }
       
    82 
       
    83     /* list of free selectors. Can be re-allocated for a period
       
    84      * of time, after which if not allocated will be closed
       
    85      * and removed from the list (by CacheCleaner thread)
       
    86      */
       
    87     LinkedList<SelectorWrapper> freeSelectors;
       
    88 
       
    89     synchronized Selector getSelector () throws IOException {
       
    90         SelectorWrapper wrapper = null;
       
    91         Selector selector;
       
    92 
       
    93         if (freeSelectors.size() > 0) {
       
    94             wrapper = freeSelectors.remove();
       
    95             selector = wrapper.getSelector();
       
    96         } else {
       
    97             selector = Selector.open();
       
    98         }
       
    99         return selector;
       
   100     }
       
   101 
       
   102     synchronized void freeSelector (Selector selector) {
       
   103         freeSelectors.add (new SelectorWrapper (selector));
       
   104     }
       
   105 
       
   106     /* Thread ensures that entries on freeSelector list
       
   107      * remain there for at least 2 minutes and no longer
       
   108      * than 4 minutes.
       
   109      */
       
   110     class CacheCleaner extends Thread {
       
   111         public void run () {
       
   112             long timeout = ServerConfig.getSelCacheTimeout() * 1000;
       
   113             while (true) {
       
   114                 try {Thread.sleep (timeout); } catch (Exception e) {}
       
   115                 synchronized (freeSelectors) {
       
   116                     ListIterator<SelectorWrapper> l = freeSelectors.listIterator();
       
   117                     while (l.hasNext()) {
       
   118                         SelectorWrapper w = l.next();
       
   119                         if (w.getDeleteFlag()) {
       
   120                             /* 2nd pass. Close the selector */
       
   121                             try {
       
   122                                 w.getSelector().close();
       
   123                             } catch (IOException e) {}
       
   124                             l.remove();
       
   125                         } else {
       
   126                             /* 1st pass. Set the flag */
       
   127                             w.setDeleteFlag (true);
       
   128                         }
       
   129                     }
       
   130                 }
       
   131             }
       
   132         }
       
   133     }
       
   134 }