java actor和线程有什么区别_akka actor模型和java thread模型各有什么优势?

从编程者角度,thread是一段顺序执行的代码,actor是一个可以接受外界消息加以处理,然后将处理结果通过消息发给外界的一个功能块。actor模型比thread更好用,并行编程的很多东西actor都帮你处理好了,只管拿来用,不用手动处理线程同步,消息收发等细节。

从操作系统/框架角度,thread的并行执行只需要进行上下文切换(context switch),actor将thread和私有数据,异步通信,事件响应有机结合在一起的一个编程模型。

至于各自优势,actor降低并行编程的门槛,可以更快速的开发出自带最佳实践的多线程程序,thread则可以让有经验的人手动处理多线程之间的同步,消息收发,定制出更符合自己需要的代码。

关于thread和actor下面这段话讲的很好

Experienced developers have learned to be very careful with unrestricted threads and various blocking mechanisms of a (real-time) operating system, because they often lead to programs that are unsafe and difficult to reason about. Instead, experts apply the following best practices of concurrent programming:

1.Keep data isolated and bound to threads. Threads should hide (encapsulate) their private data and other resources, and not share them with the rest of the system.

2.Communicate among threads asynchronously via messages (event objects). Using asynchronous events keeps the threads running truly independently, without blocking on each other.

3.Threads should spend their lifetime responding to incoming events, so their mainline should consist of an event-loop that handles events one at a time (to completion), thus avoiding any concurrency hazards within a thread itself.

While these practices can be applied manually on top of the "free threading" approach, a better way is to use the Active Object (Actor) pattern, which inherently supports and automatically enforces the best practices of concurrent programming.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值