Class AbstractThreadPoolFactory

java.lang.Object
net.algart.arrays.AbstractThreadPoolFactory
All Implemented Interfaces:
ThreadPoolFactory
Direct Known Subclasses:
DefaultThreadPoolFactory

public abstract class AbstractThreadPoolFactory extends Object implements ThreadPoolFactory

A skeletal implementation of the ThreadPoolFactory interface. Usually, you need to extend this class to implement that interface.

All non-abstract methods are completely implemented here and usually should not be overridden in subclasses.

Author:
Daniel Alievsky
  • Constructor Details

    • AbstractThreadPoolFactory

      public AbstractThreadPoolFactory()
  • Method Details

    • performTasks

      public void performTasks(Runnable[] tasks)
      Description copied from interface: ThreadPoolFactory
      Equivalent to performTasks(null, tasks) call.
      Specified by:
      performTasks in interface ThreadPoolFactory
      Parameters:
      tasks - the tasks which should be performed.
    • performTasks

      public void performTasks(ThreadFactory threadFactory, Runnable[] tasks)
      Description copied from interface: ThreadPoolFactory
      Performs the specified tasks by the thread pool, returned by getThreadPool(threadFactory) method in the beginning of execution.

      More precisely, if tasks.length==0, this method does nothing, if tasks.length==1, this method just calls tasks[0].run(), and if tasks.length>1, the tasks are performed by the following code (where pool is the result of getThreadPool(threadFactory) call):

      
       Future<?>[] results = new Future<?>[tasks.length];
       for (int threadIndex = 0; threadIndex < tasks.length; threadIndex++) {
           results[threadIndex] = pool.submit(tasks[threadIndex]);
       }
       try {
           for (int threadIndex = 0; threadIndex < tasks.length; threadIndex++) {
               results[threadIndex].get(); // waiting for finishing
           }
       } catch (ExecutionException ex) {
           Throwable cause = ex.getCause();
           if (cause instanceof RuntimeException)
               throw (RuntimeException)cause;
           if (cause instanceof Error)
               throw (Error)cause;
           throw new AssertionError("Unexpected checked exception: " + cause);
           // it is impossible, because run() method in tasks does not declare any exceptions
       } catch (InterruptedException ex) {
           throw new java.io.IOError(ex);
       }
       

      Before finishing, this method calls ThreadPoolFactory.releaseThreadPool(java.util.concurrent.ExecutorService) method for the used pool (in finally section).

      As you see, if java.util.concurrent.ExecutionException is thrown while calling one of results[...].get(), this exception is caught, and this method throws the result of its getCause() method. In other words, all possible exceptions while performing tasks are thrown as if they would be performed in the current thread.

      If java.lang.InterruptedException is thrown while calling one of results[...].get(), this exception is also caught, and this method throws java.io.IOError. (In Java 1.5, which does not support java.io.IOError, the similar net.algart.arrays.IOErrorJ5 exception is thrown instead: this branch is not listed above.) Usually, you should avoid interrupting the threads, processing AlgART arrays, via Thread.interrupt() technique (which leads to java.lang.InterruptedException): see the package description about runtime exceptions issue.

      Specified by:
      performTasks in interface ThreadPoolFactory
      Parameters:
      threadFactory - the factory, passed to ThreadPoolFactory.getThreadPool(java.util.concurrent.ThreadFactory) method to get the necessary thread pool; may be null, then some default thread factory will be used.
      tasks - the tasks which should be performed.
      See Also:
    • performTasks

      public void performTasks(Array sourceArray, ThreadFactory threadFactory, Runnable[] tasks)
      Description copied from interface: ThreadPoolFactory
      Equivalent to ThreadPoolFactory.performTasks(java.util.concurrent.ThreadFactory, Runnable[]) method with the only difference, that the thread pool is got via getThreadPool(sourceArray, threadFactory) method.
      Specified by:
      performTasks in interface ThreadPoolFactory
      Parameters:
      sourceArray - the AlgART array, passed to ThreadPoolFactory.getThreadPool(Array, java.util.concurrent.ThreadFactory) method to get the necessary thread pool.
      threadFactory - the factory, passed to ThreadPoolFactory.getThreadPool(Array, java.util.concurrent.ThreadFactory) method to get the necessary thread pool; may be null, then some default thread factory will be used.
      tasks - the tasks which should be performed.
    • performTasks

      public void performTasks(Runnable[] tasks, int from, int to)
      Description copied from interface: ThreadPoolFactory
      Equivalent to performTasks(java.util.Arrays.copyOfRange(tasks, from, to)) call.
      Specified by:
      performTasks in interface ThreadPoolFactory
      Parameters:
      tasks - the tasks which should be performed.
      from - the initial index of the performed task, inclusive
      to - the final index of the performed task, exclusive. (This index may lie outside the array.)
    • performTasks

      public void performTasks(ThreadFactory threadFactory, Runnable[] tasks, int from, int to)
      Description copied from interface: ThreadPoolFactory
      Equivalent to performTasks(threadFactory, java.util.Arrays.copyOfRange(tasks, from, to)) call.
      Specified by:
      performTasks in interface ThreadPoolFactory
      Parameters:
      tasks - the tasks which should be performed.
      from - the initial index of the performed task, inclusive
      to - the final index of the performed task, exclusive. (This index may lie outside the array.)