akka 之 Dispatcher學習筆記

來源:互聯網
上載者:User
一、概要

Dispatchers are the heart of the Akka application and this is what makes it
humming. Routers on the other hand, route incoming messages to outbound actors 二、Dispatcher 類型

Dispatcher
特點如下:

• Every actor is backed by its own mailbox
• The dispatcher can be shared with any number of actors
• The dispatcher can be backed by either thread pool or fork join pool
• The dispatcher is optimized for non-blocking code

Pinned dispatcher
特點

• Every actor is backed by its own mailbox.
• A dedicated thread for each actor implies that this dispatcher cannot be
shared with any other actors.
• The dispatcher is backed by the thread pool executor.
• The dispatcher is optimized for blocking operations. For example, if the code
is making I/O calls or database calls, then such actors will wait until the task
is finished. For such blocking operation, the pinned dispatcherperforms
better than the default dispatcher

Balancing dispatcher
特點:

• There is only one mailbox for all actors
• The dispatcher can be shared only with actors of the same type
• The dispatcher can be backed by a either thread pool or fork join pool
Calling thread dispatcher
特點:
• Every actor is backed by its own mailbox
• The dispatcher can be shared with any number of actors
• The dispatcher is backed by the calling thread 三、mailboxes 類型

• Blocking queue: Blocking queue means a queue that waits for space to
become available before putting in an element and similarly waits for t
queue to become non-empty before retrieving an element
• Bounded queue: Bounded queue means a queue that limits the size of
queue; meaning you cannot add more elements than the specified size 四、Thread pool executor 與 Fork join executor

• Thread pool executor: Here, the idea is to create a pool of worker threads.
Tasks are assigned to the pool using a queue. If the number of tasks exceeds
the number of threads, then the tasks are queued up until a thread in the
pool is available. Worker threads minimize the overhead of allocation/
deallocation of threads.
• Fork join executor: This is based on the premise of divide-and-conquer. The
idea is to divide a large task into smaller tasks whose solution can then be
combined for the final answer. The tasks need to be independentto be able
run in parallel.

計算規則:
• Minimum number of threads that will be allocated
• Maximum number of threads that will be allocated
• Multiplier factor to be used (based on number of CPU cores available)

For example, if the minimum number is defined as 3 and the multiplier factor is
2, then the dispatcher starts with a minimum of 3 x 2 = 6 threads. The maximum
number defines the upper limit on the number of threads. If the maximum number is
8, then the maximum number of threads will be 8 x 2 = 16 threads

Thread pool executor 配置方式:

# Configuration for the thread poolthread-pool-executor {# minimum number of threads core-pool-size-min = 2# available processors * factorcore-pool-size-factor = 2.0# maximum number of threads core-pool-size-max = 10}

Fork join executor 配置方式

# Configuration for the fork join poolfork-join-executor {# Min number of threads parallelism-min = 2# available processors * factorparallelism-factor = 2.0# Max number of threads parallelism-max = 10}

完整的一個dispatch 配置

my-dispatcher {type = Dispatcherexecutor = "fork-join-executor"fork-join-executor {parallelism-min = 2parallelism-factor = 2.0parallelism-max = 10}throughput = 100mailbox-capacity = -1mailbox-type =""}

代碼實現

ActorSystem _system = ActorSystem.create("dispatcher",ConfigFactory.load().getConfig("MyDispatcherExample"));ActorRef actor = _system.actorOf(new Props(MsgEchoActor.class).withDispatcher("my-dispatcher"));
五、Routers

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.