Class: Concurrent::JavaAtomicFixnum
- Inherits:
-
Object
- Object
- Concurrent::JavaAtomicFixnum
- Defined in:
- lib/concurrent/atomic/atomic_fixnum.rb
Overview
A numeric value that can be updated atomically. Reads and writes to an atomic fixnum and thread-safe and guaranteed to succeed. Reads and writes may block briefly but no explicit locking is required.
Instance Method Summary collapse
-
#compare_and_set(expect, update) ⇒ Boolean
Atomically sets the value to the given updated value if the current value == the expected value.
-
#decrement ⇒ Fixnum
(also: #down)
Decreases the current value by 1.
-
#increment ⇒ Fixnum
(also: #up)
Increases the current value by 1.
-
#initialize(init = 0) ⇒ JavaAtomicFixnum
constructor
A new instance of JavaAtomicFixnum.
-
#value ⇒ Fixnum
Retrieves the current ‘Fixnum` value.
-
#value=(value) ⇒ Fixnum
Explicitly sets the value.
Constructor Details
#initialize(init = 0) ⇒ JavaAtomicFixnum
Returns a new instance of JavaAtomicFixnum.
111 112 113 114 |
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 111 def initialize(init = 0) raise ArgumentError.new('initial value must be a Fixnum') unless init.is_a?(Fixnum) @atomic = java.util.concurrent.atomic.AtomicLong.new(init) end |
Instance Method Details
#compare_and_set(expect, update) ⇒ Boolean
Atomically sets the value to the given updated value if the current value == the expected value.
147 148 149 |
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 147 def compare_and_set(expect, update) @atomic.compare_and_set(expect, update) end |
#decrement ⇒ Fixnum Also known as: down
Decreases the current value by 1.
139 140 141 |
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 139 def decrement @atomic.decrement_and_get end |
#increment ⇒ Fixnum Also known as: up
Increases the current value by 1.
131 132 133 |
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 131 def increment @atomic.increment_and_get end |
#value ⇒ Fixnum
Retrieves the current ‘Fixnum` value.
118 119 120 |
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 118 def value @atomic.get end |
#value=(value) ⇒ Fixnum
Explicitly sets the value.
124 125 126 127 |
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 124 def value=(value) raise ArgumentError.new('value must be a Fixnum') unless value.is_a?(Fixnum) @atomic.set(value) end |