Package org.apache.sis.internal.system
Class CommonExecutor
java.lang.Object
java.lang.Number
java.util.concurrent.atomic.AtomicInteger
org.apache.sis.internal.system.CommonExecutor
- All Implemented Interfaces:
Serializable,ThreadFactory
The executor shared by most of Apache SIS library for relatively "heavy" operations.
The operations should relatively long tasks, otherwise work-stealing algorithms may
provide better performances. For example, it may be used when each computational unit
is an image tile, in which case the thread scheduling overhead is small compared to
the size of the computational task.
This thread pool is complementary to the ForkJoinPool
used by Stream API for example. The fork-join mechanism expects
computational tasks (blocking operations such as I/O are not recommended) of medium size
(between 100 and 10000 basic computational steps). By contrast, the tasks submitted to
this CommonExecutor may be long and involve blocking input/output operations.
We use a separated thread pool for avoiding the risk that long and/or blocked tasks
prevent the Java common pool from working.
This class extends AtomicInteger for opportunistic reason.
Users should not rely on this implementation details.
- Since:
- 1.1
- Version:
- 1.1
- See Also:
-
org.apache.sis.internal.gui.BackgroundThreadsForkJoinPool.commonPool()- Serialized Form
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final ThreadPoolExecutorThe executor for background tasks.static final intMaximal number of threads thatINSTANCEcan execute. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ExecutorServiceinstance()Returns the executor service shared by SIS modules for most costly calculations.Invoked byINSTANCEfor creating a new daemon thread.static booleanunschedule(Future<?> task) If the given task has been scheduled for execution but its execution did not yet started, removes it from the scheduled list.Methods inherited from class java.util.concurrent.atomic.AtomicInteger
accumulateAndGet, addAndGet, compareAndSet, decrementAndGet, doubleValue, floatValue, get, getAndAccumulate, getAndAdd, getAndDecrement, getAndIncrement, getAndSet, getAndUpdate, incrementAndGet, intValue, lazySet, longValue, set, toString, updateAndGet, weakCompareAndSetMethods inherited from class java.lang.Number
byteValue, shortValue
-
Field Details
-
PARALLELISM
public static final int PARALLELISMMaximal number of threads thatINSTANCEcan execute. If the number of tasks is greater than this parallelism value, extraneous tasks will be queued. -
INSTANCE
The executor for background tasks. The maximum number of threads is the number of processors minus 1. The minus 1 is for increasing the chances that some CPU is still available for Java common thread pool or for JavaFX/Swing thread. In addition the caller will often do part of the work in its own thread. Threads are disposed after two minutes of inactivity.
-
-
Constructor Details
-
CommonExecutor
private CommonExecutor()For the singletonINSTANCE.
-
-
Method Details
-
instance
Returns the executor service shared by SIS modules for most costly calculations.- Returns:
- the executor service for SIS tasks to run in background.
-
unschedule
If the given task has been scheduled for execution but its execution did not yet started, removes it from the scheduled list. Otherwise does nothing. The given task should be one of the following values:- The
Runnablevalue given toExecutor.execute(Runnable). - The
Futurevalue returned byExecutorService.submit(Runnable). In that case, theFuturewrapper created byThreadPoolExecutoris actually an instance ofRunnableFuture.
- Parameters:
task- the task to remove from the list of tasks to execute.- Returns:
- whether the given task has been removed.
- The
-
newThread
Invoked byINSTANCEfor creating a new daemon thread. The thread will have a slightly lower priority than normal threads so that short requests for CPU power (e.g. a user action in the JavaFX/Swing thread) are processed more quickly. This is done on the assumption that tasks executed byINSTANCEare relatively long tasks, for which loosing momentously some CPU power does not make a big difference.- Specified by:
newThreadin interfaceThreadFactory- Parameters:
r- the runnable to assign to the thread.- Returns:
- a thread for the executor.
-