使用Actors而不是`synchronized' [英] Using Actors instead of `synchronized`

查看:166
本文介绍了使用Actors而不是`synchronized'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我阅读关于在Scala中使用同步时,作者通常会提到应该使用Actors(例如)。虽然我大致了解演员的工作,我真的想看到的演员被用来替换Java的同步方法修改器的一个例子(这里我的意思是它的Scala等效 - code> synchronized block)。修改数据结构的内部实例将是很高兴看到。

Every time I read about using synchronized in Scala the author will usually mention that Actors should be used instead (this for example). While I understand roughly how actors work I'd really like to see an example of Actors being used to replace Java's synchronized method modifier (by this I mean its Scala equivalent - the synchronized block) in a piece of code. Modifying the internals of a data structure for instance would be nice to see.

这是对演员的好用还是我误导了?

Is this a good use of Actors or have I been misinformed?

推荐答案

Actor保证只有一个消息在时间处理,所以不会有两个线程访问任何实例成员 - ergo不需要使用synchronized

Actors guarantee that only a single message will be handles at time so that there will not be two threads accessing any of the instance members - ergo no need to use synchronized

这篇关于使用Actors而不是`synchronized'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆