How to designate a thread pool for actors

后端 未结 3 1274
闹比i
闹比i 2020-12-14 22:03

I have an existing java/scala application using a global thread pool. I would like to start using actors in the project but would like everything in the app using the same

3条回答
  •  伪装坚强ぢ
    2020-12-14 22:44

    But it's quite easy to re-use the thread pool used by the actor subsystem. Firstly you can control it's size:

    -Dactors.maxPoolSize=8
    

    And you can invoke work on it:

    actors.Scheduler.execute( f ); //f is => Unit
    

    The only thing it lacks is the ability to schedule work. For this I use a separate ScheduledExecutorService which is single-threaded and runs its work on the actors thread pool:

    object MyScheduler {
      private val scheduler = Executors.newSingleThreadedScheduledExecutorService
    
      def schedule(f: => Unit, delay: (Long, TimeUnit)) : ScheduledFuture[_] = {
          scheduler.schedule(new ScheduledRun(f), delay._1, delay._2)
      }
    
      private class ScheduledRun(f: => Unit) extends Runnable {
        def run = actors.Scheduler.execute(f)
      }
    
    }
    

    Then you can use this to schedule anything:

    MyScheduler.schedule(f, (60, SECONDS))
    

提交回复
热议问题