Class SyncThread<T>

  • All Implemented Interfaces:
    java.lang.Runnable

    public class SyncThread<T>
    extends SpyThread
    Thread that invokes a callable multiple times concurrently.
    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.lang.Thread

        java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.concurrent.CyclicBarrier barrier  
      private java.util.concurrent.Callable<T> callable  
      private java.util.concurrent.CountDownLatch latch  
      private T rv  
      private java.lang.Throwable throwable  
      • Fields inherited from class java.lang.Thread

        MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
    • Constructor Summary

      Constructors 
      Constructor Description
      SyncThread​(java.util.concurrent.CyclicBarrier b, java.util.concurrent.Callable<T> c)
      Get a SyncThread that will call the given callable when the given barrier allows it past.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> java.util.Collection<SyncThread<T>> getCompletedThreads​(int num, java.util.concurrent.Callable<T> callable)
      Get a collection of SyncThreads that all began as close to the same time as possible and have all completed.
      static <T> int getDistinctResultCount​(int num, java.util.concurrent.Callable<T> callable)
      Get the distinct result count for the given callable at the given concurrency.
      T getResult()
      Get the result from the invocation.
      void run()
      Wait for the barrier, invoke the callable and capture the result or an exception.
      • Methods inherited from class java.lang.Thread

        activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, toString, yield
      • Methods inherited from class java.lang.Object

        equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • callable

        private final java.util.concurrent.Callable<T> callable
      • barrier

        private final java.util.concurrent.CyclicBarrier barrier
      • latch

        private final java.util.concurrent.CountDownLatch latch
      • throwable

        private java.lang.Throwable throwable
      • rv

        private T rv
    • Constructor Detail

      • SyncThread

        public SyncThread​(java.util.concurrent.CyclicBarrier b,
                          java.util.concurrent.Callable<T> c)
        Get a SyncThread that will call the given callable when the given barrier allows it past.
        Parameters:
        b - the barrier
        c - the callable
    • Method Detail

      • run

        public void run()
        Wait for the barrier, invoke the callable and capture the result or an exception.
        Specified by:
        run in interface java.lang.Runnable
        Overrides:
        run in class java.lang.Thread
      • getResult

        public T getResult()
                    throws java.lang.Throwable
        Get the result from the invocation.
        Returns:
        the result
        Throws:
        java.lang.Throwable - if an error occurred when evaluating the callable
      • getCompletedThreads

        public static <T> java.util.Collection<SyncThread<T>> getCompletedThreads​(int num,
                                                                                  java.util.concurrent.Callable<T> callable)
                                                                           throws java.lang.InterruptedException
        Get a collection of SyncThreads that all began as close to the same time as possible and have all completed.
        Type Parameters:
        T - the result type of the SyncThread
        Parameters:
        num - the number of concurrent threads to execute
        callable - the thing to call
        Returns:
        the completed SyncThreads
        Throws:
        java.lang.InterruptedException - if we're interrupted during join
      • getDistinctResultCount

        public static <T> int getDistinctResultCount​(int num,
                                                     java.util.concurrent.Callable<T> callable)
                                              throws java.lang.Throwable
        Get the distinct result count for the given callable at the given concurrency.
        Type Parameters:
        T - the type of the callable
        Parameters:
        num - the concurrency
        callable - the callable to invoke
        Returns:
        the number of distinct (by identity) results found
        Throws:
        java.lang.Throwable - if an exception occurred in one of the invocations