Module ij
Package ij.util

Class ThreadUtil


  • public class ThreadUtil
    extends java.lang.Object
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.util.concurrent.ThreadPoolExecutor threadPoolExecutor
      The threadPoolExecutor holds at least as many threads for parallel execution as the number of processors; additional threads are added as required.
    • Constructor Summary

      Constructors 
      Constructor Description
      ThreadUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Thread[] createThreadArray()  
      static java.lang.Thread[] createThreadArray​(int nb)  
      static int getNbCpus()  
      static void joinAll​(java.util.concurrent.Future[] futures)
      Waits for completion of all Callables corresponding to the Futures given.
      static java.util.concurrent.Future[] start​(java.util.concurrent.Callable[] callables)
      Starts all callables for parallel execution (using a ThreadPoolExecutor) without waiting for the results.
      static void startAndJoin​(java.lang.Thread[] threads)
      Start all given threads and wait on each of them until all are done.
      static java.util.concurrent.Future[] startAndJoin​(java.util.concurrent.Callable[] callables)
      Starts all callables for parallel execution (using a ThreadPoolExecutor) and waits until each of them has finished.
      • Methods inherited from class java.lang.Object

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

      • threadPoolExecutor

        public static java.util.concurrent.ThreadPoolExecutor threadPoolExecutor
        The threadPoolExecutor holds at least as many threads for parallel execution as the number of processors; additional threads are added as required. These additional threads will be terminated if idle for 120 seconds.
    • Constructor Detail

      • ThreadUtil

        public ThreadUtil()
    • Method Detail

      • startAndJoin

        public static void startAndJoin​(java.lang.Thread[] threads)
        Start all given threads and wait on each of them until all are done. From Stephan Preibisch's Multithreading.java class. See: http://repo.or.cz/w/trakem2.git?a=blob;f=mpi/fruitfly/general/MultiThreading.java;hb=HEAD
        Parameters:
        threads -
      • createThreadArray

        public static java.lang.Thread[] createThreadArray​(int nb)
      • createThreadArray

        public static java.lang.Thread[] createThreadArray()
      • getNbCpus

        public static int getNbCpus()
      • startAndJoin

        public static java.util.concurrent.Future[] startAndJoin​(java.util.concurrent.Callable[] callables)
        Starts all callables for parallel execution (using a ThreadPoolExecutor) and waits until each of them has finished. If the current thread is interrupted, each of the callables gets cancelled and interrupted. Also in that case, waits until all callables have finished. The 'interrupted' status of the current thread is preserved, as required for preview in an ImageJ ExtendedPlugInFilter. Note that ImageJ requires that all callables can run concurrently, and none of them must stay in the queue while others run. (This is required by the RankFilters, where the threads are not independent)
        Parameters:
        callables - Array of tasks. If no return value is needed, best use Callable (then the Void call() method should return null). If the array size is 1, the call() method is executed in the current thread.
        Returns:
        Array of the java.util.concurrent.Futures, corresponding to the callables. If the call methods of the callables return results, the get() methods of these Futures may be used to get the results.
      • start

        public static java.util.concurrent.Future[] start​(java.util.concurrent.Callable[] callables)
        Starts all callables for parallel execution (using a ThreadPoolExecutor) without waiting for the results.
        Parameters:
        callables - Array of tasks; these might be Callable if no return value is needed (then the call methods should return null).
        Returns:
        Array of the java.util.concurrent.Futures, corresponding to the callables. The futures may be used to wait for completion of the callables or cancel them. If the call methods of the callables return results, these Futures may be used to get the results.
      • joinAll

        public static void joinAll​(java.util.concurrent.Future[] futures)
        Waits for completion of all Callables corresponding to the Futures given. If the current thread is interrupted, each of the Callables gets cancelled and interrupted. Also in that case, this method waits until all callables have finished. The 'interrupted' status of the current thread is preserved, as required for preview in an ImageJ ExtendedPlugInFilter.