Class SharedExecutorService
- All Implemented Interfaces:
AutoCloseable,Executor,ExecutorService,ScheduledExecutorService
ScheduledExecutorService that is shared by multiple consumers.
If setExecutorService(ScheduledExecutorService) isn't called a default shared executor
service is created using the following configuration settings:
azure.sdk.shared.threadpool.maxpoolsizesystem property orAZURE_SDK_SHARED_THREADPOOL_MAXPOOLSIZEenvironment variable - The maximum pool size of the shared executor service. If not set, it defaults to 10 times the number of available processors.azure.sdk.shared.threadpool.keepalivemillissystem property or {code AZURE_SDK_SHARED_THREADPOOL_KEEPALIVEMILLIS} environment variable - The keep alive time in millis for threads in the shared executor service. If not set, it defaults to 60 seconds. Limited to integer size.azure.sdk.shared.threadpool.usevirtualthreadssystem property orAZURE_SDK_SHARED_THREADPOOL_USEVIRTUALTHREADSenvironment variable - A boolean flag to indicate if the shared executor service should use virtual threads. If not set, it defaults to true. Ignored if virtual threads are not available in the runtime.
-
Method Summary
Modifier and TypeMethodDescriptionbooleanawaitTermination(long timeout, TimeUnit unit) Shutdown isn't supported for this executor service as it is shared by multiple consumers.voidGets the backing executor service for the shared instance.static SharedExecutorServiceGets the shared instance of the executor service.invokeAll(Collection<? extends Callable<T>> tasks) invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) <T> TinvokeAny(Collection<? extends Callable<T>> tasks) <T> TinvokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) booleanChecks if the executor service is shutdown.booleanChecks if the executor service is terminated.voidreset()Resets the state of thegetInstance()to an uninitialized state.<V> ScheduledFuture<V> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) voidsetExecutorService(ScheduledExecutorService executorService) Sets the backing executor service for the shared instance.voidshutdown()Shutdown isn't supported for this executor service as it is shared by multiple consumers.Shutdown isn't supported for this executor service as it is shared by multiple consumers.Future<?> <T> Future<T> <T> Future<T> Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.concurrent.ExecutorService
close
-
Method Details
-
getInstance
Gets the shared instance of the executor service.- Returns:
- The shared instance of the executor service.
-
getExecutorService
Gets the backing executor service for the shared instance.This returns the executor service for all users of the
shared instance. Meaning, if another area in code already had a reference to the shared instance.This may return null if the shared instance has not been set yet.
- Returns:
- The executor service that is set as the shared instance, may be null if a shared instance hasn't been set.
-
setExecutorService
Sets the backing executor service for the shared instance.This updates the executor service for all users of the
shared instance. Meaning, if another area in code already had a reference to the shared instance, it will now use the passed executor service to execute tasks.If the executor service is already set, this will replace it with the new executor service. If the replaced executor service was created by this class, it will be shut down.
If the passed executor service is null, this will throw a
NullPointerException. If the passed executor service is shutdown or terminated, this will throw anIllegalStateException.- Parameters:
executorService- The executor service to set as the shared instance.- Throws:
NullPointerException- If the passed executor service is null.IllegalStateException- If the passed executor service is shutdown or terminated.
-
reset
public void reset()Resets the state of thegetInstance()to an uninitialized state.This will shut down the executor service if it was created by this class.
-
shutdown
public void shutdown()Shutdown isn't supported for this executor service as it is shared by multiple consumers.Calling this method will result in an
UnsupportedOperationExceptionbeing thrown.- Specified by:
shutdownin interfaceExecutorService- Throws:
UnsupportedOperationException- This method will always throw an exception.
-
shutdownNow
Shutdown isn't supported for this executor service as it is shared by multiple consumers.Calling this method will result in an
UnsupportedOperationExceptionbeing thrown.- Specified by:
shutdownNowin interfaceExecutorService- Returns:
- Nothing will be returned as an exception will always be thrown.
- Throws:
UnsupportedOperationException- This method will always throw an exception.
-
isShutdown
public boolean isShutdown()Checks if the executor service is shutdown.Will always return false as the shared executor service cannot be shut down.
- Specified by:
isShutdownin interfaceExecutorService- Returns:
- False, as the shared executor service cannot be shut down.
-
isTerminated
public boolean isTerminated()Checks if the executor service is terminated.Will always return false as the shared executor service cannot be terminated.
- Specified by:
isTerminatedin interfaceExecutorService- Returns:
- False, as the shared executor service cannot be terminated.
-
awaitTermination
Shutdown isn't supported for this executor service as it is shared by multiple consumers.Calling this method will result in an
UnsupportedOperationExceptionbeing thrown.- Specified by:
awaitTerminationin interfaceExecutorService- Parameters:
timeout- The amount of time to wait for the executor service to shutdown.unit- The unit of time for the timeout.- Returns:
- Nothing will be returned as an exception will always be thrown.
- Throws:
UnsupportedOperationException- This method will always throw an exception.
-
execute
-
submit
- Specified by:
submitin interfaceExecutorService
-
submit
- Specified by:
submitin interfaceExecutorService
-
submit
- Specified by:
submitin interfaceExecutorService
-
invokeAll
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException - Specified by:
invokeAllin interfaceExecutorService- Throws:
InterruptedException
-
invokeAll
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException - Specified by:
invokeAllin interfaceExecutorService- Throws:
InterruptedException
-
invokeAny
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException - Specified by:
invokeAnyin interfaceExecutorService- Throws:
InterruptedExceptionExecutionException
-
invokeAny
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException - Specified by:
invokeAnyin interfaceExecutorService- Throws:
InterruptedExceptionExecutionExceptionTimeoutException
-
schedule
- Specified by:
schedulein interfaceScheduledExecutorService
-
schedule
- Specified by:
schedulein interfaceScheduledExecutorService
-
scheduleAtFixedRate
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) - Specified by:
scheduleAtFixedRatein interfaceScheduledExecutorService
-
scheduleWithFixedDelay
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) - Specified by:
scheduleWithFixedDelayin interfaceScheduledExecutorService
-