importscala.concurrent.stm._valx=Ref(0)// allocate a Ref[Int]
valy=Ref.make[String]()// type-specific default
valz=x.single// Ref.View[Int]
atomic{implicittxn=>vali=x()// read
y()="x was "+i// write
valeq=atomic{implicittxn=>// nested atomic
x()==z()// both Ref and Ref.View can be used inside atomic
}assert(eq)y.set(y.get+", long-form access")}// only Ref.View can be used outside atomic
println("y was '"+y.single()+"'")println("z was "+z())atomic{implicittxn=>y()=y()+", first alternative"if(xgetWith{_>0})// read via a function
retry// try alternatives or block
}orAtomic{implicittxn=>y()=y()+", second alternative"}valprev=z.swap(10)// atomic swap
valsuccess=z.compareAndSet(10,11)// atomic compare-and-set
z.transform{_max20}// atomic transformation
valpre=y.single.getAndTransform{_.toUpperCase}valpost=y.single.transformAndGet{_.filterNot{_==' '}}