ConnectionRunnerPool.java 4.55 KB
Newer Older
Tom Tromey committed
1 2 3 4 5 6 7 8 9
/* gnu.java.rmi.server.ConnectionRunnerPool
   Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
10

Tom Tromey committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


package gnu.java.rmi.server;

import java.util.ArrayList;
import java.util.Arrays;

//Should I generalize this class?

class ConnectionRunnerPool
{
48 49

  public static
Tom Tromey committed
50 51 52
    class ConnectionRunner extends Thread{
      private UnicastConnection conn;
      private volatile boolean exiting = false;
53

Tom Tromey committed
54 55 56
      public ConnectionRunner(ThreadGroup group, String id){
        super(group, id);
      }
57

Tom Tromey committed
58 59
      public synchronized void run(){
        while(!exiting){
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
          if(conn == null)
            try{
              wait();
            }catch(InterruptedException e){
              continue;
            }
          else{
            conn.run();
            conn = null;
            synchronized(ConnectionRunnerPool.class){
              freelist.add(this);
              if(freelist.size() == 1)
                ConnectionRunnerPool.class.notifyAll();
            }
          }
Tom Tromey committed
75 76
        }
      }
77

Tom Tromey committed
78 79 80 81
      public synchronized void dispatch(UnicastConnection conn){
        this.conn = conn;
        notify();
      }
82

Tom Tromey committed
83 84 85
      void exit(){
        exiting = true;
        if(conn != null)
86 87 88
          try{
            join(500);
          }catch(InterruptedException e){}
Tom Tromey committed
89 90
        interrupt();
      }
91

Tom Tromey committed
92
    }
93

Tom Tromey committed
94 95 96
  // Should this value equal to number of CPU?
  private static int size = 5;
  private static int max_size = 10;
97

Tom Tromey committed
98 99
  // Package-private to avoid a trampoline.
  static ArrayList freelist;
100

Tom Tromey committed
101
  private static ThreadGroup group = new ThreadGroup("pool");
102

Tom Tromey committed
103 104 105
  static {
    ConnectionRunner[] pools = new ConnectionRunner[size];
    for(int i = 0; i < pools.length; i++){
106
      pools[i] = new ConnectionRunner(group, Integer.toString(i));
Tom Tromey committed
107 108 109 110 111
      pools[i].setContextClassLoader(Thread.currentThread().getContextClassLoader());
      pools[i].start();
    }
    freelist = new ArrayList(Arrays.asList(pools));
  }
112

Tom Tromey committed
113 114 115
  public static void setSize(int size_){
    size = size_;
  }
116

Tom Tromey committed
117 118 119
  public static void setMaxSize(int size){
    max_size = size;
  }
120

Tom Tromey committed
121 122 123 124
  private static synchronized ConnectionRunner getConnectionRunner()
  {
    if(freelist.size() == 0){
      if(size < max_size){
125 126 127 128
        ++size;
        ConnectionRunner a = new ConnectionRunner(group, Integer.toString(size));
        a.start();
        freelist.add(a);
Tom Tromey committed
129
      }else
130 131 132 133
        while(freelist.size() == 0)
          try{
            ConnectionRunnerPool.class.wait();
          }catch(InterruptedException e){}
Tom Tromey committed
134
    }
135

Tom Tromey committed
136 137 138 139 140
    // always let the first in pool most busy or other scheduling plan??
    ConnectionRunner a = (ConnectionRunner)freelist.get(0);
    freelist.remove(a);
    return a;
  }
141

Tom Tromey committed
142 143 144 145 146
  public static void dispatchConnection(UnicastConnection conn)
  {
    ConnectionRunner r = getConnectionRunner();
    r.dispatch(conn);
  }
147

Tom Tromey committed
148 149 150 151 152 153 154
  public static void exit()
  {
    Thread[] list = new Thread[group.activeCount()];
    group.enumerate(list);
    for(int i = 0; i < list.length; i++)
      ((ConnectionRunner)list[i]).exit();
  }
155

Tom Tromey committed
156
}