View | Details | Raw Unified | Return to bug 52996
Collapse All | Expand All

(-)java/org/apache/catalina/core/StandardThreadExecutor.java (-4 / +62 lines)
Lines 33-52 Link Here
33
public class StandardThreadExecutor implements Executor {
33
public class StandardThreadExecutor implements Executor {
34
    
34
    
35
    // ---------------------------------------------- Properties
35
    // ---------------------------------------------- Properties
36
    /**
37
     * Default thread priority
38
     */
36
    protected int threadPriority = Thread.NORM_PRIORITY;
39
    protected int threadPriority = Thread.NORM_PRIORITY;
37
40
41
    /**
42
     * Run threads in daemon or non-daemon state
43
     */
38
    protected boolean daemon = true;
44
    protected boolean daemon = true;
39
    
45
    
46
    /**
47
     * Default name prefix for the thread name
48
     */
40
    protected String namePrefix = "tomcat-exec-";
49
    protected String namePrefix = "tomcat-exec-";
41
    
50
    
51
    /**
52
     * max number of threads
53
     */
42
    protected int maxThreads = 200;
54
    protected int maxThreads = 200;
43
    
55
    
56
    /**
57
     * min number of threads
58
     */
44
    protected int minSpareThreads = 25;
59
    protected int minSpareThreads = 25;
45
    
60
    
61
    /**
62
     * idle time in milliseconds
63
     */
46
    protected int maxIdleTime = 60000;
64
    protected int maxIdleTime = 60000;
47
    
65
    
66
    /**
67
     * The executor we use for this component
68
     */
48
    protected ThreadPoolExecutor executor = null;
69
    protected ThreadPoolExecutor executor = null;
49
    
70
    
71
    /**
72
     * the name of this thread pool
73
     */
50
    protected String name;
74
    protected String name;
51
    
75
    
52
    /**
76
    /**
Lines 54-59 Link Here
54
     */
78
     */
55
    protected AtomicInteger submittedTasksCount;
79
    protected AtomicInteger submittedTasksCount;
56
    
80
    
81
    /**
82
     * The maximum number of elements that can queue up before we reject them
83
     */
84
    protected int maxQueueSize = Integer.MAX_VALUE;
85
    
57
    private LifecycleSupport lifecycle = new LifecycleSupport(this);
86
    private LifecycleSupport lifecycle = new LifecycleSupport(this);
58
    // ---------------------------------------------- Constructors
87
    // ---------------------------------------------- Constructors
59
    public StandardThreadExecutor() {
88
    public StandardThreadExecutor() {
Lines 65-71 Link Here
65
    // ---------------------------------------------- Public Methods
94
    // ---------------------------------------------- Public Methods
66
    public void start() throws LifecycleException {
95
    public void start() throws LifecycleException {
67
        lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);
96
        lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);
68
        TaskQueue taskqueue = new TaskQueue();
97
        TaskQueue taskqueue = new TaskQueue(maxQueueSize);
69
        TaskThreadFactory tf = new TaskThreadFactory(namePrefix);
98
        TaskThreadFactory tf = new TaskThreadFactory(namePrefix);
70
        lifecycle.fireLifecycleEvent(START_EVENT, null);
99
        lifecycle.fireLifecycleEvent(START_EVENT, null);
71
        executor = new ThreadPoolExecutor(getMinSpareThreads(), getMaxThreads(), maxIdleTime, TimeUnit.MILLISECONDS,taskqueue, tf) {
100
        executor = new ThreadPoolExecutor(getMinSpareThreads(), getMaxThreads(), maxIdleTime, TimeUnit.MILLISECONDS,taskqueue, tf) {
Lines 91-96 Link Here
91
        lifecycle.fireLifecycleEvent(AFTER_STOP_EVENT, null);
120
        lifecycle.fireLifecycleEvent(AFTER_STOP_EVENT, null);
92
    }
121
    }
93
    
122
    
123
    public void execute(Runnable command, long timeout, TimeUnit unit) {
124
        if ( executor != null ) {
125
            try {
126
                executor.execute(command);
127
            } catch (RejectedExecutionException rx) {
128
                //there could have been contention around the queue
129
                try {
130
                    if ( !( (TaskQueue) executor.getQueue()).force(command,timeout,unit) ) throw new RejectedExecutionException("Work queue full.");
131
                }catch (InterruptedException x) {
132
                    throw new RejectedExecutionException("Interrupted.",x);
133
                }
134
            }
135
        } else throw new IllegalStateException("StandardThreadPool not started.");
136
    }
137
    
138
    
94
    public void execute(Runnable command) {
139
    public void execute(Runnable command) {
95
        if ( executor != null ) {
140
        if ( executor != null ) {
96
        	submittedTasksCount.incrementAndGet();
141
        	submittedTasksCount.incrementAndGet();
Lines 100-106 Link Here
100
                //there could have been contention around the queue
145
                //there could have been contention around the queue
101
                if ( !( (TaskQueue) executor.getQueue()).force(command) ) {
146
                if ( !( (TaskQueue) executor.getQueue()).force(command) ) {
102
                	submittedTasksCount.decrementAndGet();
147
                	submittedTasksCount.decrementAndGet();
103
                	throw new RejectedExecutionException();
148
                	throw new RejectedExecutionException("Work queue full.");
104
                }
149
                }
105
            }
150
            }
106
        } else throw new IllegalStateException("StandardThreadPool not started.");
151
        } else throw new IllegalStateException("StandardThreadPool not started.");
Lines 172-177 Link Here
172
        this.name = name;
217
        this.name = name;
173
    }
218
    }
174
    
219
    
220
    public void setMaxQueueSize(int size) {
221
        this.maxQueueSize = size;
222
    }
223
    
224
    public int getMaxQueueSize() {
225
        return maxQueueSize;
226
    }
227
    
175
    /**
228
    /**
176
     * Add a LifecycleEvent listener to this component.
229
     * Add a LifecycleEvent listener to this component.
177
     *
230
     *
Lines 233-240 Link Here
233
            super();
286
            super();
234
        }
287
        }
235
288
236
        public TaskQueue(int initialCapacity) {
289
        public TaskQueue(int capacity) {
237
            super(initialCapacity);
290
            super(capacity);
238
        }
291
        }
239
292
240
        public TaskQueue(Collection<? extends Runnable> c) {
293
        public TaskQueue(Collection<? extends Runnable> c) {
Lines 250-255 Link Here
250
            return super.offer(o); //forces the item onto the queue, to be used if the task is rejected
303
            return super.offer(o); //forces the item onto the queue, to be used if the task is rejected
251
        }
304
        }
252
305
306
        public boolean force(Runnable o, long timeout, TimeUnit unit) throws InterruptedException {
307
            if ( parent.isShutdown() ) throw new RejectedExecutionException("Executor not running, can't force a command into the queue");
308
            return super.offer(o,timeout,unit); //forces the item onto the queue, to be used if the task is rejected
309
        }
310
253
        public boolean offer(Runnable o) {
311
        public boolean offer(Runnable o) {
254
            //we can't do any checks
312
            //we can't do any checks
255
            if (parent==null) return super.offer(o);
313
            if (parent==null) return super.offer(o);
(-)java/org/apache/catalina/Executor.java (+18 lines)
Lines 16-23 Link Here
16
 */
16
 */
17
package org.apache.catalina;
17
package org.apache.catalina;
18
18
19
import java.util.concurrent.RejectedExecutionException;
20
import java.util.concurrent.TimeUnit;
19
21
20
22
23
21
public interface Executor extends java.util.concurrent.Executor, Lifecycle {
24
public interface Executor extends java.util.concurrent.Executor, Lifecycle {
22
    public String getName();
25
    public String getName();
26
    
27
    /**
28
     * Executes the given command at some time in the future.  The command
29
     * may execute in a new thread, in a pooled thread, or in the calling
30
     * thread, at the discretion of the <tt>Executor</tt> implementation.
31
     * If no threads are available, it will be added to the work queue.
32
     * If the work queue is full, the system will wait for the specified 
33
     * time until it throws a RejectedExecutionException
34
     *
35
     * @param command the runnable task
36
     * @throws RejectedExecutionException if this task cannot be
37
     * accepted for execution - the queue is full
38
     * @throws NullPointerException if command or unit is null
39
     */
40
    void execute(Runnable command, long timeout, TimeUnit unit);
23
}
41
}

Return to bug 52996