Module ij
Package ij.util

# Class ThreadUtil

java.lang.Object
ij.util.ThreadUtil

public class ThreadUtil extends Object

## Field Summary

Fields
Modifier and Type
Field
Description
`static 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

Modifier and Type
Method
Description
`static Thread[]`
`createThreadArray()`
`static Thread[]`
`createThreadArray(int nb)`
`static int`
`getNbCpus()`
`static void`
`joinAll(Future[] futures)`
Waits for completion of all `Callable`s corresponding to the `Future`s given.
`static Future[]`
`start(Callable[] callables)`
Starts all callables for parallel execution (using a ThreadPoolExecutor) without waiting for the results.
`static void`
`startAndJoin(Thread[] threads)`
Start all given threads and wait on each of them until all are done.
`static Future[]`
`startAndJoin(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 Details

### threadPoolExecutor

public static 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 Details

### ThreadUtil

public ThreadUtil()

## Method Details

### startAndJoin

public static void startAndJoin(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 Thread[] createThreadArray(int nb)

### createThreadArray

public static Thread[] createThreadArray()

### getNbCpus

public static int getNbCpus()

### startAndJoin

public static Future[] startAndJoin(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<Void>` (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.Future`s, 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 Future[] start(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<Void>` if no return value is needed (then the `call` methods should return null).
Returns:
  Array of the `java.util.concurrent.Future`s, 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(Future[] futures)
Waits for completion of all `Callable`s corresponding to the `Future`s given. If the current thread is interrupted, each of the `Callable`s 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.