PageRenderTime 63ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/concurrent/java/ActorPool.java

https://bitbucket.org/bedlaczech/fan-1.0
Java | 143 lines | 96 code | 21 blank | 26 comment | 10 complexity | 70d4c40b46680ad9bb87e8fa3d7f246d MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //
  2. // Copyright (c) 2009, Brian Frank and Andy Frank
  3. // Licensed under the Academic Free License version 3.0
  4. //
  5. // History:
  6. // 26 Mar 09 Brian Frank Creation
  7. //
  8. package fan.concurrent;
  9. import fan.sys.*;
  10. /**
  11. * Controller for a group of actors which manages their execution
  12. * using pooled thread resources.
  13. */
  14. public class ActorPool
  15. extends FanObj
  16. {
  17. //////////////////////////////////////////////////////////////////////////
  18. // Construction
  19. //////////////////////////////////////////////////////////////////////////
  20. public static ActorPool make() { return make(null); }
  21. public static ActorPool make(Func func)
  22. {
  23. ActorPool self = new ActorPool();
  24. make$(self, func);
  25. return self;
  26. }
  27. public static void make$(ActorPool self) { make$(self, null); }
  28. public static void make$(ActorPool self, Func itBlock)
  29. {
  30. if (itBlock != null)
  31. {
  32. itBlock.enterCtor(self);
  33. itBlock.call(self);
  34. itBlock.exitCtor();
  35. }
  36. if (self.maxThreads < 1) throw ArgErr.make("ActorPool.maxThreads must be >= 1, not " + self.maxThreads);
  37. self.threadPool = new ThreadPool(self.name, (int)self.maxThreads);
  38. self.scheduler = new Scheduler(self.name);
  39. }
  40. //////////////////////////////////////////////////////////////////////////
  41. // Obj
  42. //////////////////////////////////////////////////////////////////////////
  43. public Type typeof()
  44. {
  45. if (type == null) type = Type.find("concurrent::ActorPool");
  46. return type;
  47. }
  48. private static Type type;
  49. //////////////////////////////////////////////////////////////////////////
  50. // ActorPool
  51. //////////////////////////////////////////////////////////////////////////
  52. public final boolean isStopped()
  53. {
  54. return threadPool.isStopped();
  55. }
  56. public final boolean isDone()
  57. {
  58. return threadPool.isDone();
  59. }
  60. public final ActorPool stop()
  61. {
  62. scheduler.stop();
  63. threadPool.stop();
  64. return this;
  65. }
  66. public final ActorPool kill()
  67. {
  68. killed = true;
  69. scheduler.stop();
  70. threadPool.kill();
  71. return this;
  72. }
  73. public final ActorPool join() { return join(null); }
  74. public final ActorPool join(Duration timeout)
  75. {
  76. if (!isStopped()) throw Err.make("ActorPool is not stopped");
  77. long ms = timeout == null ? Long.MAX_VALUE : timeout.millis();
  78. try
  79. {
  80. if (threadPool.join(ms)) return this;
  81. }
  82. catch (InterruptedException e)
  83. {
  84. throw InterruptedErr.make(e);
  85. }
  86. throw TimeoutErr.make("ActorPool.join timed out");
  87. }
  88. public Object trap(String name, List args)
  89. {
  90. if (name.equals("dump")) { threadPool.dump(args); return null; }
  91. return super.trap(name, args);
  92. }
  93. final void submit(Actor actor)
  94. {
  95. threadPool.submit(actor);
  96. }
  97. final void schedule(Actor a, Duration d, Future f)
  98. {
  99. scheduler.schedule(d.ticks(), new ScheduledWork(a, f));
  100. }
  101. //////////////////////////////////////////////////////////////////////////
  102. // ScheduledWork
  103. //////////////////////////////////////////////////////////////////////////
  104. static class ScheduledWork implements Scheduler.Work
  105. {
  106. ScheduledWork(Actor a, Future f) { actor = a; future = f; }
  107. public String toString() { return "ScheduledWork msg=" + future.msg; }
  108. public void work() { if (!future.isCancelled()) actor._enqueue(future, false); }
  109. public void cancel() { future.cancel(); }
  110. final Actor actor;
  111. final Future future;
  112. }
  113. //////////////////////////////////////////////////////////////////////////
  114. // Fields
  115. //////////////////////////////////////////////////////////////////////////
  116. private ThreadPool threadPool;
  117. private Scheduler scheduler;
  118. volatile boolean killed;
  119. public String name = "ActorPool";
  120. public long maxThreads = 100;
  121. public long maxMsgsBeforeYield = 100;
  122. }