Class ConcurrentUtil

java.lang.Object
ca.spottedleaf.concurrentutil.util.ConcurrentUtil

public final class ConcurrentUtil extends Object
  • Constructor Details

    • ConcurrentUtil

      public ConcurrentUtil()
  • Method Details

    • genericToString

      public static String genericToString(Object object)
    • rethrow

      public static void rethrow(Throwable exception)
    • getVarHandle

      public static VarHandle getVarHandle(Class<?> lookIn, String fieldName, Class<?> fieldType)
    • getStaticVarHandle

      public static VarHandle getStaticVarHandle(Class<?> lookIn, String fieldName, Class<?> fieldType)
    • backoff

      public static void backoff()
      Non-exponential backoff algorithm to use in lightly contended areas.
      See Also:
    • exponentiallyBackoffSimple

      public static long exponentiallyBackoffSimple(long counter)
      Backoff algorithm to use for a short held lock (i.e compareAndExchange operation). Generally this should not be used when a thread can block another thread. Instead, use exponentiallyBackoffComplex(long).
      Parameters:
      counter - The current counter.
      Returns:
      The counter plus 1.
      See Also:
    • exponentiallyBackoffComplex

      public static long exponentiallyBackoffComplex(long counter)
      Backoff algorithm to use for a lock that can block other threads (i.e if another thread contending with this thread can be thrown off the scheduler). This lock should not be used for simple locks such as compareAndExchange.
      Parameters:
      counter - The current counter.
      Returns:
      The next (if any) step in the backoff logic.
      See Also:
    • linearLongBackoff

      public static long linearLongBackoff(long counter, long scale, long max)
      Simple exponential backoff that will linearly increase the time per failure, according to the scale.
      Parameters:
      counter - The current failure counter.
      scale - Time per failure, in ns.
      max - The maximum time to wait for, in ns.
      Returns:
      The next counter.
    • linearLongBackoffDeadline

      public static long linearLongBackoffDeadline(long counter, long scale, long max, long deadline)
      Simple exponential backoff that will linearly increase the time per failure, according to the scale.
      Parameters:
      counter - The current failure counter.
      scale - Time per failure, in ns.
      max - The maximum time to wait for, in ns.
      deadline - The deadline in ns. Deadline time source: System.nanoTime().
      Returns:
      The next counter.
    • getArrayHandle

      public static VarHandle getArrayHandle(Class<?> type)