blob: 568a045631676a02ba2aa2638f063b902a06070b (
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
86
87
88
89
90
91
92
93
94
95
96
97
98
|
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2005-2009, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
// $Id$
package scala.actors.scheduler
import java.util.concurrent.ThreadPoolExecutor
/**
* The <code>ThreadPoolScheduler</code> class uses an
* <code>ThreadPoolExecutor</code> to execute <code>Actor</code>s.
*
* A <code>ThreadPoolScheduler</code> attempts to shut down
* the underlying <code>ExecutorService</code> only if
* <code>terminate</code> is set to true.
*
* Otherwise, the <code>ExecutorService</code> must be shut down either
* directly or by shutting down the
* <code>ThreadPoolScheduler</code> instance.
*
* @author Philipp Haller
*/
class ThreadPoolScheduler(protected var executor: ThreadPoolExecutor,
protected var terminate: Boolean)
extends Thread with TerminationMonitor with ExecutorScheduler {
private var terminating = false
protected val CHECK_FREQ = 10
/* This constructor (and the var above) is currently only used to work
* around a bug in scaladoc, which cannot deal with early initializers
* (to be used in subclasses such as DefaultExecutorScheduler) properly.
*/
def this() {
this(null, true)
}
override def managedBlock(blocker: ManagedBlocker) {
val coreSize = executor.getCorePoolSize()
if ((executor.getActiveCount() >= coreSize - 1) && coreSize < ThreadPoolConfig.maxPoolSize) {
executor.setCorePoolSize(coreSize + 1)
}
blocker.block()
}
override def run() {
try {
while (true) {
this.synchronized {
try {
wait(CHECK_FREQ)
} catch {
case _: InterruptedException =>
}
if (terminating)
throw new QuitException
if (terminate && allTerminated)
throw new QuitException
val coreSize = executor.getCorePoolSize()
if ((executor.getActiveCount() >= coreSize - 1) && coreSize < ThreadPoolConfig.maxPoolSize) {
executor.setCorePoolSize(coreSize + 1)
}
}
}
} catch {
case _: QuitException =>
Debug.info(this+": initiating shutdown...")
// invoke shutdown hook
onShutdown()
// allow thread to exit
}
}
/** Submits a closure for execution.
*
* @param fun the closure to be executed
*/
def execute(fun: => Unit): Unit =
execute(new Runnable {
def run() { fun }
})
/** Shuts down the scheduler.
*/
def shutdown(): Unit = synchronized {
terminating = true
}
}
|