Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to designate a thread pool for actors

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 pool.

I know I can set the maximum number of threads that actors use but would prefer sharing the thread pool. Is this necessary/reasonable, and is it possible to designate the actor's thread pool?

If it is not possible/recommended, are there any rules of thumb when integrating actors in apps that are already using threads?

Thanks.

like image 662
agilefall Avatar asked Oct 20 '09 23:10

agilefall


People also ask

How do you make a thread pool?

To use thread pools, we first create a object of ExecutorService and pass a set of tasks to it. ThreadPoolExecutor class allows to set the core and maximum pool size. The runnables that are run by a particular thread are executed sequentially.

What is the point of a thread pool?

A thread pool is a collection of worker threads that efficiently execute asynchronous callbacks on behalf of the application. The thread pool is primarily used to reduce the number of application threads and provide management of the worker threads.

Should you use a thread pool?

A thread pool helps mitigate the issue of performance by reducing the number of threads needed and managing their lifecycle. Essentially, threads are kept in the thread pool until they're needed, after which they execute the task and return the pool to be reused later.


1 Answers

I believe you can do something like this:

trait MyActor extends Actor {
  val pool = ... // git yer thread pool here
  override def scheduler = new SchedulerAdapter {
    def execute(block: => Unit) =
      pool.execute(new Runnable {
        def run() { block }
      })
  }
} 
like image 107
Mitch Blevins Avatar answered Sep 22 '22 05:09

Mitch Blevins