summaryrefslogtreecommitdiff
path: root/src/actors/scala/actors/DefaultExecutorScheduler.scala
blob: 22da121bbca31d346cc3ee1e09d53002a99e8657 (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
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2005-2009, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

// $Id$

package scala.actors

import java.util.concurrent.{ThreadPoolExecutor, TimeUnit, LinkedBlockingQueue}

/**
 * The <code>DefaultExecutorScheduler</code> class uses a default
 * <code>ThreadPoolExecutor</code> for executing <code>Actor</code>s.
 *
 * It can be configured using the two JVM properties
 * <code>actors.corePoolSize</code> and
 * <code>actors.maxPoolSize</code> that control the initial and
 * maximum size of the thread pool, respectively.
 *
 * @author Philipp Haller
 */
class DefaultExecutorScheduler extends {

  private val rt = Runtime.getRuntime()
  private val minNumThreads = 4

  /** The value of the actors.corePoolSize JVM property. This property
   *  determines the initial thread pool size.
   */
  private val coreProp = try {
    System.getProperty("actors.corePoolSize")
  } catch {
    case ace: java.security.AccessControlException =>
      null
  }

  private val maxProp =
    try {
      System.getProperty("actors.maxPoolSize")
    } catch {
      case ace: java.security.AccessControlException =>
        null
    }

  private val initCoreSize =
    if (null ne coreProp) Integer.parseInt(coreProp)
    else {
      val numCores = rt.availableProcessors()
      if (2 * numCores > minNumThreads)
        2 * numCores
      else
        minNumThreads
    }

  private val maxSize =
    if (null ne maxProp) Integer.parseInt(maxProp)
    else 256

  private val coreSize = initCoreSize

  private val workQueue = new LinkedBlockingQueue[Runnable]

  private val threadPool = new ThreadPoolExecutor(coreSize,
                                                  maxSize,
                                                  50L,
                                                  TimeUnit.MILLISECONDS,
                                                  workQueue)
  } with ExecutorScheduler(threadPool) {
    override val CHECK_FREQ = 50
  }