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

来源:互联网 发布:淘宝卖家钱多久到账 编辑:程序博客网 时间:2024/05/29 16:32

//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.

0 0
原创粉丝点击