123456789_123456789_123456789_123456789_123456789_

Class: Concurrent::JavaExchanger

Overview

Note:

**Private Implementation:** This abstraction is a private, internal implementation detail. It should never be used directly.

Constant Summary

AbstractExchanger - Inherited

CANCEL

Class Attribute Summary

Class Method Summary

AbstractExchanger - Inherited

Synchronization::Object - Inherited

.atomic_attribute?, .atomic_attributes,
.attr_atomic

Creates methods for reading and writing to a instance variable with volatile (Java) semantic as .attr_volatile does.

.attr_volatile

Creates methods for reading and writing (as attr_accessor does) to a instance variable with volatile (Java) semantic.

.ensure_safe_initialization_when_final_fields_are_present

For testing purposes, quite slow.

.new

Has to be called by children.

.safe_initialization!, .define_initialize_atomic_fields

Synchronization::AbstractObject - Inherited

Instance Method Summary

AbstractExchanger - Inherited

#exchange

Waits for another thread to arrive at this exchange point (unless the current thread is interrupted), and then transfers the given object to it, receiving its object in return.

#exchange!

Waits for another thread to arrive at this exchange point (unless the current thread is interrupted), and then transfers the given object to it, receiving its object in return.

#try_exchange

Waits for another thread to arrive at this exchange point (unless the current thread is interrupted), and then transfers the given object to it, receiving its object in return.

#do_exchange

Waits for another thread to arrive at this exchange point (unless the current thread is interrupted), and then transfers the given object to it, receiving its object in return.

Synchronization::Object - Inherited

Synchronization::Volatile - Included

Synchronization::AbstractObject - Inherited

Constructor Details

.newJavaExchanger

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/exchanger.rb', line 298

def initialize
  @exchanger = java.util.concurrent.Exchanger.new
end

Instance Method Details

#do_exchange(value, timeout) ⇒ Object, CANCEL (private)

Waits for another thread to arrive at this exchange point (unless the current thread is interrupted), and then transfers the given object to it, receiving its object in return. The timeout value indicates the approximate number of seconds the method should block while waiting for the exchange. When the timeout value is nil the method will block indefinitely.

Parameters:

  • value (Object)

    the value to exchange with another thread

  • timeout (Numeric, nil)

    in seconds, nil blocks indefinitely

Returns:

  • (Object, CANCEL)

    the value exchanged by the other thread; CANCEL on timeout

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/exchanger.rb', line 307

def do_exchange(value, timeout)
  result = nil
  if timeout.nil?
    Synchronization::JRuby.sleep_interruptibly do
      result = @exchanger.exchange(value)
    end
  else
    Synchronization::JRuby.sleep_interruptibly do
      result = @exchanger.exchange(value, 1000 * timeout, java.util.concurrent.TimeUnit::MILLISECONDS)
    end
  end
  result
rescue java.util.concurrent.TimeoutException
  CANCEL
end