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.
Testing with ruby 2.1.2
Testing with Concurrent::MutexAtomicFixnum...
3.130000 0.000000 3.130000 ( 3.136505)
Testing with Concurrent::CAtomicFixnum...
0.790000 0.000000 0.790000 ( 0.785550)
Testing with jruby 1.9.3
Testing with Concurrent::MutexAtomicFixnum...
5.460000 2.460000 7.920000 ( 3.715000)
Testing with Concurrent::JavaAtomicFixnum...
4.520000 0.030000 4.550000 ( 1.187000)
Constant Summary collapse
- MIN_VALUE =
Java::JavaLang::Long::MIN_VALUE
- MAX_VALUE =
Java::JavaLang::Long::MAX_VALUE
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
Creates a new ‘AtomicFixnum` with the given initial value.
-
#value ⇒ Fixnum
Retrieves the current ‘Fixnum` value.
-
#value=(value) ⇒ Fixnum
Explicitly sets the value.
Constructor Details
#initialize(init = 0) ⇒ JavaAtomicFixnum
Creates a new ‘AtomicFixnum` with the given initial value.
131 132 133 134 |
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 131 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.
160 161 162 |
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 160 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.
154 155 156 |
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 154 def decrement @atomic.decrement_and_get end |
#increment ⇒ Fixnum Also known as: up
Increases the current value by 1.
148 149 150 |
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 148 def increment @atomic.increment_and_get end |
#value ⇒ Fixnum
Retrieves the current ‘Fixnum` value.
137 138 139 |
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 137 def value @atomic.get end |
#value=(value) ⇒ Fixnum
Explicitly sets the value.
142 143 144 145 |
# File 'lib/concurrent/atomic/atomic_fixnum.rb', line 142 def value=(value) raise ArgumentError.new('value must be a Fixnum') unless value.is_a?(Fixnum) @atomic.set(value) end |