Class: Concurrent::JavaAtomicBoolean

Inherits:
Object
  • Object
show all
Defined in:
lib/concurrent/atomic/atomic_boolean.rb

Overview

A boolean value that can be updated atomically. Reads and writes to an atomic boolean 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::MutexAtomicBoolean...
  2.790000   0.000000   2.790000 (  2.791454)
Testing with Concurrent::CAtomicBoolean...
  0.740000   0.000000   0.740000 (  0.740206)

Testing with jruby 1.9.3
Testing with Concurrent::MutexAtomicBoolean...
  5.240000   2.520000   7.760000 (  3.683000)
Testing with Concurrent::JavaAtomicBoolean...
  3.340000   0.010000   3.350000 (  0.855000)

Instance Method Summary collapse

Constructor Details

#initialize(initial = false) ⇒ JavaAtomicBoolean

Creates a new ‘AtomicBoolean` with the given initial value.

Parameters:

  • initial (Boolean) (defaults to: false)

    the initial value



124
125
126
# File 'lib/concurrent/atomic/atomic_boolean.rb', line 124

def initialize(initial = false)
  @atomic = java.util.concurrent.atomic.AtomicBoolean.new(!!initial)
end

Instance Method Details

#false?Boolean

Is the current value ‘false`

Returns:

  • (Boolean)

    true if the current value is ‘false`, else false



146
147
148
# File 'lib/concurrent/atomic/atomic_boolean.rb', line 146

def false?
  !@atomic.get
end

#make_falseBoolean

Explicitly sets the value to false.

Returns:

  • (Boolean)

    true is value has changed, otherwise false



156
157
158
# File 'lib/concurrent/atomic/atomic_boolean.rb', line 156

def make_false
  @atomic.compareAndSet(true, false)
end

#make_trueBoolean

Explicitly sets the value to true.

Returns:

  • (Boolean)

    true is value has changed, otherwise false



151
152
153
# File 'lib/concurrent/atomic/atomic_boolean.rb', line 151

def make_true
  @atomic.compareAndSet(false, true)
end

#true?Boolean

Is the current value ‘true`

Returns:

  • (Boolean)

    true if the current value is ‘true`, else false



141
142
143
# File 'lib/concurrent/atomic/atomic_boolean.rb', line 141

def true?
  @atomic.get
end

#valueBoolean

Retrieves the current ‘Boolean` value.

Returns:

  • (Boolean)

    the current value



130
131
132
# File 'lib/concurrent/atomic/atomic_boolean.rb', line 130

def value
  @atomic.get
end

#value=(value) ⇒ Boolean

Explicitly sets the value.

Parameters:

  • value (Boolean)

    the new value to be set

Returns:

  • (Boolean)

    the current value



136
137
138
# File 'lib/concurrent/atomic/atomic_boolean.rb', line 136

def value=(value)
  @atomic.set(!!value)
end