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:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final ThreadPoolExecutor
The executor for background tasks.static final int
Maximal number of threads thatINSTANCE
can execute. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ExecutorService
instance()
Returns the executor service shared by SIS modules for most costly calculations.Invoked byINSTANCE
for creating a new daemon thread.static boolean
unschedule
(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, compareAndExchange, compareAndExchangeAcquire, compareAndExchangeRelease, compareAndSet, decrementAndGet, doubleValue, floatValue, get, getAcquire, getAndAccumulate, getAndAdd, getAndDecrement, getAndIncrement, getAndSet, getAndUpdate, getOpaque, getPlain, incrementAndGet, intValue, lazySet, longValue, set, setOpaque, setPlain, setRelease, toString, updateAndGet, weakCompareAndSet, weakCompareAndSetAcquire, weakCompareAndSetPlain, weakCompareAndSetRelease, weakCompareAndSetVolatile
Methods inherited from class java.lang.Number
byteValue, shortValue
-
Field Details
-
PARALLELISM
public static final int PARALLELISMMaximal number of threads thatINSTANCE
can 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
Runnable
value given toExecutor.execute(Runnable)
. - The
Future
value returned byExecutorService.submit(Runnable)
. In that case, theFuture
wrapper created byThreadPoolExecutor
is 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 byINSTANCE
for 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 byINSTANCE
are relatively long tasks, for which loosing momentously some CPU power does not make a big difference.- Specified by:
newThread
in interfaceThreadFactory
- Parameters:
r
- the runnable to assign to the thread.- Returns:
- a thread for the executor.
-