Class AbstractThreadPoolFactory
- All Implemented Interfaces:
ThreadPoolFactory
- Direct Known Subclasses:
DefaultThreadPoolFactory
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 Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
performTasks
(Runnable[] tasks) Equivalent toperformTasks
(null, tasks) call.void
performTasks
(Runnable[] tasks, int from, int to) Equivalent toperformTasks
(java.util.Arrays.copyOfRange(tasks, from, to)) call.void
performTasks
(ThreadFactory threadFactory, Runnable[] tasks) Performs the specified tasks by the thread pool, returned bygetThreadPool(threadFactory)
method in the beginning of execution.void
performTasks
(ThreadFactory threadFactory, Runnable[] tasks, int from, int to) Equivalent toperformTasks
(threadFactory, java.util.Arrays.copyOfRange(tasks, from, to)) call.void
performTasks
(Array sourceArray, ThreadFactory threadFactory, Runnable[] tasks) Equivalent toThreadPoolFactory.performTasks(java.util.concurrent.ThreadFactory, Runnable[])
method with the only difference, that the thread pool is got viagetThreadPool(sourceArray, threadFactory)
method.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface net.algart.arrays.ThreadPoolFactory
getThreadPool, getThreadPool, recommendedNumberOfTasks, recommendedNumberOfTasks, releaseThreadPool, singleThreadVersion
-
Constructor Details
-
AbstractThreadPoolFactory
public AbstractThreadPoolFactory()
-
-
Method Details
-
performTasks
Description copied from interface:ThreadPoolFactory
Equivalent toperformTasks
(null, tasks) call.- Specified by:
performTasks
in interfaceThreadPoolFactory
- Parameters:
tasks
- the tasks which should be performed.
-
performTasks
Description copied from interface:ThreadPoolFactory
Performs the specified tasks by the thread pool, returned bygetThreadPool(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 interfaceThreadPoolFactory
- Parameters:
threadFactory
- the factory, passed toThreadPoolFactory.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
Description copied from interface:ThreadPoolFactory
Equivalent toThreadPoolFactory.performTasks(java.util.concurrent.ThreadFactory, Runnable[])
method with the only difference, that the thread pool is got viagetThreadPool(sourceArray, threadFactory)
method.- Specified by:
performTasks
in interfaceThreadPoolFactory
- Parameters:
sourceArray
- the AlgART array, passed toThreadPoolFactory.getThreadPool(Array, java.util.concurrent.ThreadFactory)
method to get the necessary thread pool.threadFactory
- the factory, passed toThreadPoolFactory.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
Description copied from interface:ThreadPoolFactory
Equivalent toperformTasks
(java.util.Arrays.copyOfRange(tasks, from, to)) call.- Specified by:
performTasks
in interfaceThreadPoolFactory
- Parameters:
tasks
- the tasks which should be performed.from
- the initial index of the performed task, inclusiveto
- the final index of the performed task, exclusive. (This index may lie outside the array.)
-
performTasks
Description copied from interface:ThreadPoolFactory
Equivalent toperformTasks
(threadFactory, java.util.Arrays.copyOfRange(tasks, from, to)) call.- Specified by:
performTasks
in interfaceThreadPoolFactory
- Parameters:
tasks
- the tasks which should be performed.from
- the initial index of the performed task, inclusiveto
- the final index of the performed task, exclusive. (This index may lie outside the array.)
-