KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > threadpool > MinPooledExecutor


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util.threadpool;
23
24 import EDU.oswego.cs.dl.util.concurrent.PooledExecutor;
25 import EDU.oswego.cs.dl.util.concurrent.Channel;
26
27 /** A pooled executor where the minimum pool size threads are kept alive. This
28 is needed in order for the waitWhenBlocked option to work because of a
29 race condition inside the Executor. The race condition goes something like:
30
31 RT - Requesting Thread wanting to use the pool
32 LT - Last Thread in the pool
33
34 RT: Check there are enough free threads to process,
35    yes LT is there, so no need to create a new thread.
36 LT: Times out on the keep alive, LT is destroyed.
37 RT: Try to execute, blocks because there are no available threads.
38    In fact, the pool is now empty which the executor mistakenly
39    inteprets as all of them being in use.
40
41 Doug Lea says he isn't going to fix. In fact, the version in j2se
42 1.5 doesn't have this option. In order for this to work, the min pool
43 size must be > 0.
44
45 @author Scott.Stark@jboss.org
46 @author adrian@jboss.org
47 @version $Revision: 1958 $
48  */

49 public class MinPooledExecutor extends PooledExecutor
50 {
51    // Constants -----------------------------------------------------
52

53
54    // Attributes ----------------------------------------------------
55

56    /** The number of threads to keep alive threads */
57    protected int keepAliveSize;
58
59    // Static --------------------------------------------------------
60

61    // Constructors --------------------------------------------------
62

63    /**
64     * Construct a new executor
65     *
66     * @param poolSize the maximum pool size
67     */

68    public MinPooledExecutor(int poolSize)
69    {
70       super(poolSize);
71    }
72    
73    /**
74     * Construct a new executor
75     *
76     * @param channel the queue for any requests
77     * @param poolSize the maximum pool size
78     */

79    public MinPooledExecutor(Channel channel, int poolSize)
80    {
81       super(channel, poolSize);
82    }
83
84    // Public --------------------------------------------------------
85

86    /**
87     * @return the number of threads to keep alive
88     */

89    public int getKeepAliveSize()
90    {
91       return keepAliveSize;
92    }
93
94    /**
95     * @param keepAliveSize the number of threads to keep alive
96     */

97    public void setKeepAliveSize(int keepAliveSize)
98    {
99       this.keepAliveSize = keepAliveSize;
100    }
101
102    // PooledExecutor overrides --------------------------------------
103

104    protected Runnable JavaDoc getTask() throws InterruptedException JavaDoc
105    {
106       Runnable JavaDoc task = super.getTask();
107       while (task == null && keepAlive())
108       {
109          task = super.getTask();
110       }
111       return task;
112    }
113
114    // Package protected ---------------------------------------------
115

116    // Protected -----------------------------------------------------
117

118    /**
119     * We keep alive unless we are told to shutdown
120     * or there are more than keepAliveSize threads in the pool
121     *
122     * @return whether to keep alive
123     */

124    protected synchronized boolean keepAlive()
125    {
126       if (shutdown_)
127          return false;
128
129       return poolSize_ <= keepAliveSize;
130    }
131
132    // Private -------------------------------------------------------
133

134    // Inner classes -------------------------------------------------
135
}
136
Popular Tags