Class Cache.Work

All Implemented Interfaces:
Comparable<Delayed>, Runnable, Delayed, Supplier<V>, Cache.Handler<V>
Enclosing class:
Cache<K,V>

final class Cache.Work extends DelayedRunnable.Immediate implements Cache.Handler<V>, Supplier<V>
A handler implementation used for telling to other threads that the current thread is computing a value.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    (package private) final class 
    A handler implementation used when the value is in process of being computed in another thread.

    Nested classes/interfaces inherited from class org.apache.sis.internal.system.DelayedRunnable

    DelayedRunnable.Immediate
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    (package private) final K
    The key to use for storing the result in the map.
    (package private) final ReentrantLock
    The synchronization lock.
    private V
    The result.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Work(K key)
    Creates a new handler which will store the result in the given map at the given key.
  • Method Summary

    Modifier and Type
    Method
    Description
    get()
    Waits for the completion of the value computation and returns this result.
    Usually returns null since the value is not yet computed.
    void
    putAndUnlock(V result)
    Stores the result and release the lock.
    void
    run()
    Invoked in a background thread after a value has been set in the map.

    Methods inherited from class org.apache.sis.internal.system.DelayedRunnable.Immediate

    compareTo, getDelay

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • lock

      final ReentrantLock lock
      The synchronization lock.
    • key

      final K key
      The key to use for storing the result in the map.
    • value

      private V value
      The result. This is initially null, as we expect since the value has not yet been created. When it will get a value, this value should not change anymore.
  • Constructor Details

    • Work

      Work(K key)
      Creates a new handler which will store the result in the given map at the given key.
  • Method Details

    • get

      public V get()
      Waits for the completion of the value computation and returns this result. This method should be invoked only from another thread than the one doing the computation.
      Specified by:
      get in interface Supplier<V>
    • peek

      public V peek()
      Usually returns null since the value is not yet computed. May returns the result if this method is invoked again after the computation, but this is not the typical use case.
      Specified by:
      peek in interface Cache.Handler<V>
      Returns:
      the value from the cache, or null if none.
    • putAndUnlock

      public void putAndUnlock(V result) throws IllegalStateException
      Stores the result and release the lock.
      Specified by:
      putAndUnlock in interface Cache.Handler<V>
      Parameters:
      result - the result to store in the cache, or null for removing the entry from the cache. If an entry is removed, a new computation will be attempted the next time a handler is created for the same key.
      Throws:
      IllegalStateException - if the current thread does not hold the lock.
    • run

      public void run()
      Invoked in a background thread after a value has been set in the map. This method computes a cost estimation of the new value. If the total cost is greater than the cost limit, then oldest strong references are replaced by weak references.
      Specified by:
      run in interface Runnable