summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/util/GrowingThreadPoolExecutor.java
blob: 7b0b2d9c708f55e73e6127e35d1cb744b4885a8e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package org.openslx.util;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Grows to maximum pool size before queueing. See
 * http://stackoverflow.com/a/20153234/2043481
 */
public class GrowingThreadPoolExecutor extends ThreadPoolExecutor {
	private int userSpecifiedCorePoolSize;
	private int taskCount;
	
    /**
     * The default rejected execution handler
     */
    private static final RejectedExecutionHandler defaultHandler =
        new AbortPolicy();

	public GrowingThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
			TimeUnit unit, BlockingQueue<Runnable> workQueue) {
		this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, defaultHandler);
	}
	
	public GrowingThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
			BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
		this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, defaultHandler);
	}

	public GrowingThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
			BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
		this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(),
				handler);
	}
	
	public GrowingThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
			BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
		super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
		userSpecifiedCorePoolSize = corePoolSize;
	}

	@Override
	public void execute(Runnable runnable) {
		synchronized (this) {
			taskCount++;
			setCorePoolSizeToTaskCountWithinBounds();
		}
		super.execute(runnable);
	}

	@Override
	protected void afterExecute(Runnable runnable, Throwable throwable) {
		super.afterExecute(runnable, throwable);
		synchronized (this) {
			taskCount--;
			setCorePoolSizeToTaskCountWithinBounds();
		}
	}

	private void setCorePoolSizeToTaskCountWithinBounds() {
		int threads = taskCount;
		if (threads < userSpecifiedCorePoolSize)
			threads = userSpecifiedCorePoolSize;
		if (threads > getMaximumPoolSize())
			threads = getMaximumPoolSize();
		super.setCorePoolSize(threads);
	}
	
	public void setCorePoolSize(int corePoolSize) {
		synchronized (this) {
			userSpecifiedCorePoolSize = corePoolSize;
		}
	}
	
	@Override
	public int getCorePoolSize() {
		synchronized (this) {
			return userSpecifiedCorePoolSize;
		}
	}
}