Class ThreadUtils
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic <T> scala.concurrent.AwaitableawaitReady(scala.concurrent.Awaitable<T> awaitable, scala.concurrent.duration.Duration atMost) Preferred alternative toAwait.ready().static <T> TawaitResult(Future<T> future, scala.concurrent.duration.Duration atMost) static <T> TawaitResult(scala.concurrent.Awaitable<T> awaitable, scala.concurrent.duration.Duration atMost) Preferred alternative toAwait.result().static ThreadFactorynamedThreadFactory(String prefix) Create a thread factory that names threads with a prefix and also sets the threads to daemon.static ExecutorServicenewDaemonBlockingThreadPoolExecutorService(int nThreads, int workQueueSize, String prefix) Simliar to newDaemonFixedThreadPool, but with a bound workQueue, task submission will be blocked when queue is full.static ThreadPoolExecutornewDaemonCachedThreadPool(String prefix) Wrapper over newCachedThreadPool.static ThreadPoolExecutornewDaemonCachedThreadPool(String prefix, int maxThreadNumber, int keepAliveSeconds) Create a cached thread pool whose max number of threads ismaxThreadNumber.static ThreadPoolExecutornewDaemonFixedThreadPool(int nThreads, String prefix) Wrapper over newFixedThreadPool.static ThreadPoolExecutornewDaemonSingleThreadExecutor(String threadName) Wrapper over newFixedThreadPool with single daemon thread.static ThreadPoolExecutornewDaemonSingleThreadExecutorWithRejectedExecutionHandler(String threadName, int taskQueueCapacity, RejectedExecutionHandler rejectedExecutionHandler) Wrapper over newSingleThreadExecutor that allows the specification of a RejectedExecutionHandlerstatic ScheduledExecutorServicenewDaemonSingleThreadScheduledExecutor(String threadName) Wrapper over ScheduledThreadPoolExecutor the pool with daemon threads.static ScheduledExecutorServicenewDaemonThreadPoolScheduledExecutor(String threadNamePrefix, int numThreads) Wrapper over ScheduledThreadPoolExecutor.static ForkJoinPoolnewForkJoinPool(String prefix, int maxThreadNumber) Construct a new ForkJoinPool with a specified max parallelism and name prefix.static ScheduledThreadPoolExecutornewSingleThreadScheduledExecutor(String threadName) Wrapper over ScheduledThreadPoolExecutor the pool with non-daemon threads.static <I,O> scala.collection.immutable.Seq<O> Transforms input collection by applying the given function to each element in parallel fashion.static <T> TrunInNewThread(String threadName, boolean isDaemon, scala.Function0<T> body) Run a piece of code in a new thread and return the result.static scala.concurrent.ExecutionContextExecutorAnExecutionContextExecutorthat runs each task in the thread that invokesexecute/submit.static ExecutorServicestatic voidshutdown(ExecutorService executor, scala.concurrent.duration.Duration gracePeriod) static <T extends Throwable>
 TwrapCallerStacktrace(T realException, String combineMessage, int dropStacks) Adjust exception stack stace to wrap with caller side thread stack trace.
- 
Constructor Details- 
ThreadUtilspublic ThreadUtils()
 
- 
- 
Method Details- 
sameThreadExecutorService
- 
sameThreadpublic static scala.concurrent.ExecutionContextExecutor sameThread()AnExecutionContextExecutorthat runs each task in the thread that invokesexecute/submit. The caller should make sure the tasks running in thisExecutionContextExecutorare short and never block.- Returns:
- (undocumented)
 
- 
namedThreadFactoryCreate a thread factory that names threads with a prefix and also sets the threads to daemon.- Parameters:
- prefix- (undocumented)
- Returns:
- (undocumented)
 
- 
newDaemonCachedThreadPoolWrapper over newCachedThreadPool. Thread names are formatted as prefix-ID, where ID is a unique, sequentially assigned integer.- Parameters:
- prefix- (undocumented)
- Returns:
- (undocumented)
 
- 
newDaemonCachedThreadPoolpublic static ThreadPoolExecutor newDaemonCachedThreadPool(String prefix, int maxThreadNumber, int keepAliveSeconds) Create a cached thread pool whose max number of threads ismaxThreadNumber. Thread names are formatted as prefix-ID, where ID is a unique, sequentially assigned integer.- Parameters:
- prefix- (undocumented)
- maxThreadNumber- (undocumented)
- keepAliveSeconds- (undocumented)
- Returns:
- (undocumented)
 
- 
newDaemonFixedThreadPoolWrapper over newFixedThreadPool. Thread names are formatted as prefix-ID, where ID is a unique, sequentially assigned integer.- Parameters:
- nThreads- (undocumented)
- prefix- (undocumented)
- Returns:
- (undocumented)
 
- 
newDaemonSingleThreadExecutorWrapper over newFixedThreadPool with single daemon thread.- Parameters:
- threadName- (undocumented)
- Returns:
- (undocumented)
 
- 
newDaemonSingleThreadExecutorWithRejectedExecutionHandlerpublic static ThreadPoolExecutor newDaemonSingleThreadExecutorWithRejectedExecutionHandler(String threadName, int taskQueueCapacity, RejectedExecutionHandler rejectedExecutionHandler) Wrapper over newSingleThreadExecutor that allows the specification of a RejectedExecutionHandler- Parameters:
- threadName- (undocumented)
- taskQueueCapacity- (undocumented)
- rejectedExecutionHandler- (undocumented)
- Returns:
- (undocumented)
 
- 
newDaemonBlockingThreadPoolExecutorServicepublic static ExecutorService newDaemonBlockingThreadPoolExecutorService(int nThreads, int workQueueSize, String prefix) Simliar to newDaemonFixedThreadPool, but with a bound workQueue, task submission will be blocked when queue is full.- Parameters:
- nThreads- the number of threads in the pool
- workQueueSize- the capacity of the queue to use for holding tasks before they are executed. Task submission will be blocked when queue is full.
- prefix- thread names are formatted as prefix-ID, where ID is a unique, sequentially assigned integer.
- Returns:
- BlockingThreadPoolExecutorService
 
- 
newDaemonSingleThreadScheduledExecutorWrapper over ScheduledThreadPoolExecutor the pool with daemon threads.- Parameters:
- threadName- (undocumented)
- Returns:
- (undocumented)
 
- 
newSingleThreadScheduledExecutorWrapper over ScheduledThreadPoolExecutor the pool with non-daemon threads.- Parameters:
- threadName- (undocumented)
- Returns:
- (undocumented)
 
- 
newDaemonThreadPoolScheduledExecutorpublic static ScheduledExecutorService newDaemonThreadPoolScheduledExecutor(String threadNamePrefix, int numThreads) Wrapper over ScheduledThreadPoolExecutor.- Parameters:
- threadNamePrefix- (undocumented)
- numThreads- (undocumented)
- Returns:
- (undocumented)
 
- 
runInNewThreadRun a piece of code in a new thread and return the result. Exception in the new thread is thrown in the caller thread with an adjusted stack trace that removes references to this method for clarity. The exception stack traces will be like the following:SomeException: exception-message at CallerClass.body-method (sourcefile.scala) at ... run in separate thread using org.apache.spark.util.ThreadUtils ... () at CallerClass.caller-method (sourcefile.scala) ... - Parameters:
- threadName- (undocumented)
- isDaemon- (undocumented)
- body- (undocumented)
- Returns:
- (undocumented)
 
- 
wrapCallerStacktracepublic static <T extends Throwable> T wrapCallerStacktrace(T realException, String combineMessage, int dropStacks) Adjust exception stack stace to wrap with caller side thread stack trace. The exception stack traces will be like the following:SomeException: exception-message at CallerClass.body-method (sourcefile.scala) at ... run in separate thread using org.apache.spark.util.ThreadUtils ... () at CallerClass.caller-method (sourcefile.scala) ... - Parameters:
- realException- (undocumented)
- combineMessage- (undocumented)
- dropStacks- (undocumented)
- Returns:
- (undocumented)
 
- 
newForkJoinPoolConstruct a new ForkJoinPool with a specified max parallelism and name prefix.- Parameters:
- prefix- (undocumented)
- maxThreadNumber- (undocumented)
- Returns:
- (undocumented)
 
- 
awaitResultpublic static <T> T awaitResult(scala.concurrent.Awaitable<T> awaitable, scala.concurrent.duration.Duration atMost) throws SparkException Preferred alternative toAwait.result().This method wraps and re-throws any exceptions thrown by the underlying Awaitcall, ensuring that this thread's stack trace appears in logs.In addition, it calls Awaitable.resultdirectly to avoid usingForkJoinPool'sBlockingContext. Codes running in the user's thread may be in a thread of Scala ForkJoinPool. As concurrent executions in ForkJoinPool may see someThreadLocalvalue unexpectedly, this method basically prevents ForkJoinPool from running other tasks in the current waiting thread. In general, we should use this method because many places in Spark useThreadLocaland it's hard to debug whenThreadLocals leak to other tasks.- Parameters:
- awaitable- (undocumented)
- atMost- (undocumented)
- Returns:
- (undocumented)
- Throws:
- SparkException
 
- 
awaitResultpublic static <T> T awaitResult(Future<T> future, scala.concurrent.duration.Duration atMost) throws SparkException - Throws:
- SparkException
 
- 
awaitReadypublic static <T> scala.concurrent.Awaitable awaitReady(scala.concurrent.Awaitable<T> awaitable, scala.concurrent.duration.Duration atMost) throws SparkException Preferred alternative toAwait.ready().- Parameters:
- awaitable- (undocumented)
- atMost- (undocumented)
- Returns:
- (undocumented)
- Throws:
- SparkException
- See Also:
 
- 
shutdownpublic static void shutdown(ExecutorService executor, scala.concurrent.duration.Duration gracePeriod) 
- 
parmappublic static <I,O> scala.collection.immutable.Seq<O> parmap(scala.collection.immutable.Seq<I> in, String prefix, int maxThreads, scala.Function1<I, O> f) Transforms input collection by applying the given function to each element in parallel fashion. Comparing to the map() method of Scala parallel collections, this method can be interrupted at any time. This is useful on canceling of task execution, for example.Functions are guaranteed to be executed in freshly-created threads that inherit the calling thread's Spark thread-local variables. These threads also inherit the calling thread's active SparkSession. - Parameters:
- in- - the input collection which should be transformed in parallel.
- prefix- - the prefix assigned to the underlying thread pool.
- maxThreads- - maximum number of thread can be created during execution.
- f- - the lambda function will be applied to each element of- in.
- Returns:
- new collection in which each element was given from the input collection inby applying the lambda functionf.
 
 
-