您的位置:首页 > 其它

知识库--Using Akka Refs and Transactions(124)

2017-02-26 08:02 393 查看
//Akka – Ref

Unlike Clojure, where refs were defined at the language level, Akka can’trely on any existing language support. Instead, Akka provides, as part of the akka.stm package, a managed transactional reference Ref and specialized classes for primitive types such as IntRef, LongRef, and so on. The Ref (and the specialized references) represents the managed mutable identity to an immutable value of type T. Types like Integer, Long, Double, String, and other immutable types fit the bill well to serve as value objects. If we use one of our own classes, we must make sure it’s immutable, that is, that it contains only final fields.

//Ref的使用get() swap()

We can create a managed transactional reference, which is an instance of Ref, by providing an initial value or by omitting the value, defaulting to null. To obtain the current value from the reference, use the get() method. To change the mutable identity to point to a new value, use the swap() method. These calls are performed within the transaction we provide or in their own individual transactions if none is provided.

//并发处理 HB原则

When multiple threads try to change the same managed reference, Akka ensures that changes by one are written to memory and the others are retried. The transactional facility takes care of crossing the memory barriers. That is, Akka, through Multiverse, guarantees that the changes to a managed ref committed by a transaction happen before, and are visible to, any reads of the same ref later in other transactions.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  并发
相关文章推荐