akka 之 Dispatcher学习笔记

一、概要

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 类型

  1. 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
    这里写图片描述

  2. 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
    这里写图片描述

  3. 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
    这里写图片描述

  4. 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 pool
thread-pool-executor {
# minimum number of threads 
core-pool-size-min = 2
# available processors * factor
core-pool-size-factor = 2.0
# maximum number of threads 
core-pool-size-max = 10
}

Fork join executor 配置方式

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

完整的一个dispatch 配置

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

代码实现

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

五、Routers

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值