tinyscalautils.threads
Members list
Type members
Classlikes
Implicit use of timers.
Attributes
- Source
- Executors.scala
- Supertypes
- Self type
-
Execute.type
Attributes
- Source
- Executors.scala
- Supertypes
- Self type
-
ExecuteAfter.type
A factory for customized thread pools.
A factory for customized thread pools.
It makes it easier to specify a rejected execution handler (including DiscardPolicy
) and/or a thread factory (including defaultThreadFactory
). Furthermore, cached thread pools are created with a keepalive time of 1 second instead of 1 minute, which is useful in tests and demos that cannot conveniently shut down a thread pool. Finally, thread pools are returned as instances of ExecutionContextExecutorService
, which makes them suitable for use with Java or Scala constructs.
Instances of this class are immutable. They are created through the companion object, e.g.:
val p1 = Executors.silent.newUnlimitedThreadPool()
val p2 = Executors.silent.withFactory(tf).newThreadPool(4)
Attributes
- Since
-
1.0
- Companion
- object
- Source
- Executors.scala
- Supertypes
- Known subtypes
-
object Executors
Companion object.
Companion object.
It is itself an instance of Executors
that uses the default rejected execution handler and thread factory.
Attributes
- Since
-
1.0
- Companion
- class
- Source
- Executors.scala
- Supertypes
- Self type
-
Executors.type
A thread factory that keeps a reference on all the threads it creates.
A thread factory that keeps a reference on all the threads it creates.
The factory can be reset: threads created before the reset are discarded.
Instances of KeepThreadsFactory
are thread-safe, and allThreads
can be invoked while the factory is still being used to create more threads.
Attributes
- See also
- Since
-
1.0
- Companion
- object
- Source
- KeepThreadsFactory.scala
- Supertypes
Factory methods.
Factory methods.
Attributes
- Since
-
1.0
- Companion
- class
- Source
- KeepThreadsFactory.scala
- Supertypes
- Self type
-
KeepThreadsFactory.type
A marker trait for test threads.
A marker trait for test threads.
This can be used as a cheap alternative to KeepThreadsFactory to keep track of threads created by a factory, as a group.
Attributes
- See also
- Since
-
1.0
- Source
- MarkedThread.scala
- Supertypes
A factory of marked threads.
A factory of marked threads.
The threads produced by this factory have the MarkedThread marker and are named MarkedThread-<count>
.
Attributes
- Since
-
1.0
- Companion
- object
- Source
- MarkedThreadFactory.scala
- Supertypes
- Known subtypes
-
object MarkedThreadFactory
A default marked thread factory.
A default marked thread factory.
Attributes
- Companion
- class
- Source
- MarkedThreadFactory.scala
- Supertypes
- Self type
-
MarkedThreadFactory.type
Attributes
- Source
- Executors.scala
- Supertypes
- Self type
-
Run.type
Attributes
- Source
- Executors.scala
- Supertypes
- Self type
-
RunAfter.type
Simple timers.
Value members
Concrete methods
A Kotlin-like function to create threads.
A Kotlin-like function to create threads.
Empty names are ignored.
Attributes
- Since
-
1.0
- Source
- newThread.scala
A Kotlin-like function to create threads.
A Kotlin-like function to create threads.
This is the short form of newThread
that uses default values.
Attributes
- Since
-
1.0
- Source
- newThread.scala
Runs code on the given pool and interrupts runner when interrupted. If interrupted while waiting for code to complete, an InterruptedException
is thrown.
Runs code on the given pool and interrupts runner when interrupted. If interrupted while waiting for code to complete, an InterruptedException
is thrown.
Attributes
- Since
-
1.1
- Source
- runAsync.scala
The given thread pool.
The given thread pool.
For instance:
withThreads(16):
val exec = theThreads
...
Attributes
- Since
-
1.7
- Note
-
This is the same as
summon[ExecutionContextExecutorService]
. - Source
- withThreads.scala
Runs code within an implicit execution context.
Runs code within an implicit execution context.
If the code produces a future (or more generally, an Awaitable
), this construct waits for completion of this future and returns its value. Otherwise, it returns the value of the code itself.
For example:
val result = withThreads(myContext, shutdown = true):
val f = Future { ... }
val g = f.map(...)
g.filter(...)
If shutdown
is true, the execution context must be a subtype of ExecutorService
and is shutdown, but not waited on.
Attributes
- Since
-
1.6
- Source
- withThreads.scala
Runs code within an implicit execution context.
Runs code within an implicit execution context.
This is equivalent to withThreads(executor, shutdown = false)(code)
.
Attributes
- Since
-
1.6
- Source
- withThreads.scala
Runs code within a newly created implicit thread pool.
Runs code within a newly created implicit thread pool.
If the code produces a future (or more generally, an Awaitable
), this construct waits for completion of this future and returns its value. Otherwise, it returns the value of the code itself.
The thread pool is shutdown and, if awaitTermination
is true, the code waits for its termination.
For example:
val result = withThreads(maxThreads = 8, awaitTermination = true):
val f = Future { ... }
val g = f.map(...)
g.filter(...)
Attributes
- Throws
-
IllegalArgumentException
if
maxThreads
is not positive. - Since
-
1.6
- Source
- withThreads.scala
Runs code within a newly created implicit thread pool.
Runs code within a newly created implicit thread pool.
This is equivalent to withThreads(maxThreads, awaitTermination = false)(code)
.
Attributes
- Since
-
1.6
- Source
- withThreads.scala
Runs code within a newly created implicit unlimited thread pool.
Runs code within a newly created implicit unlimited thread pool.
The behavior is the same as withThreads except that the thread pool is unlimited.
Attributes
- Since
-
1.6
- Note
-
This is not quite equivalent to
withThreads(someLargeNumber, awaitTermination)(code)
because the keepalive time is 1 second on unlimited thread pools but is unlimited on bounded pools. - Source
- withThreads.scala
Runs code within a newly created implicit unlimited thread pool.
Runs code within a newly created implicit unlimited thread pool.
This is equivalent to withThreads(awaitTermination = false)(code)
.
Attributes
- Since
-
1.6
- Source
- withThreads.scala
Deprecated methods
Attributes
- Deprecated
-
[Since version 1.6]
- Source
- withThreads.scala
Attributes
- Deprecated
-
[Since version 1.6]
- Source
- withThreads.scala
Attributes
- Deprecated
-
[Since version 1.6]
- Source
- withThreads.scala
Attributes
- Deprecated
-
[Since version 1.6]
- Source
- withThreads.scala
Attributes
- Deprecated
-
[Since version 1.6]
- Source
- withThreads.scala
Attributes
- Deprecated
-
[Since version 1.6]
- Source
- withThreads.scala
Concrete fields
The number of processors (cores) available, as reported by Runtime.availableProcessors
.
The number of processors (cores) available, as reported by Runtime.availableProcessors
.
Attributes
- Source
- availableProcessors.scala
Givens
Givens
A single-thread timer.
A single-thread timer.
The thread is created in daemon mode.
Attributes
- Since
-
1.0
- Source
- timeouts.scala
Extensions
Extensions
Same as standard await
but in seconds.
Floating seconds version of awaitTermination
.
Floating seconds version of awaitTermination
.
Value parameters
- seconds
-
timeout, in seconds.
Attributes
- See also
- Since
-
1.0
- Source
- extensions.scala
Shuts down the executor and waits for termination.
Shuts down the executor and waits for termination.
If the executor fails to terminate (before a timeout or an interrupt) and force
is set to true, invokes shutdownNow
.
Value parameters
- force
-
if true,
shutdownNow
is invoked after a timeout or an interrupt. - seconds
-
timeout, in seconds.
Attributes
- Returns
-
true if the executor terminates before the timeout.
- Since
-
1.0
- Source
- extensions.scala
Shuts down the executor and waits forever for termination.
Shuts down the executor and waits forever for termination.
Attributes
- Returns
-
true.
- Since
-
1.0
- Source
- extensions.scala
Shuts down the executor and waits forever for termination.
Shuts down the executor and waits forever for termination.
Value parameters
- force
-
if true,
shutdownNow
is invoked after a timeout.
Attributes
- Returns
-
true.
- Since
-
1.7
- Source
- extensions.scala
Allows an unevaluated argument to replace an explicit Runnable
.
Allows an unevaluated argument to replace an explicit Runnable
.
Instead of exec.execute(() => code)
, you can write exec.run(code)
.
Attributes
- Note
-
code
is rejected if it has typeRunnable
orCallable
because it's probably a mistake:exec.run(code)
was written whenexec.execute(code)
orexec.run(code.run())
orexec.run(code.call())
was intended. In the unlikely case thatexec.run(code)
was desired, add a type ascription:exec.run(code: AnyRef)
. - Source
- Executors.scala
Adds a completeOnTimeout method similar to Java's java.util.concurrent.CompletableFuture#completeOnTimeout.
Adds a completeOnTimeout method similar to Java's java.util.concurrent.CompletableFuture#completeOnTimeout.
The new future has the same outcome, unless the computation times out. In that case, the future is completed by executing the fallback code, passed by name. The fallback code runs in the execution context (not the timer).
Value parameters
- fallbackCode
-
code to produce a fallback value in case of timeout.
- seconds
-
timeout, in seconds.
- strict
-
This flag controls the race between timeout and normal completion. If false, the race is between normal completion and termination of the fallback code. In other words, the fallback computation is initiated at the timeout, but the original computation can still complete while the fallback code is running (in which case the fallback value is computed but not used). If true, the race is between normal completion and initiation of the fallback code. Once the fallback code is started, its value will be used to complete the future, even if normal completion of the initial task occurs in the meantime.
- timer
-
a timer used to complete the future after the timeout. For convenience, timeoutTimer can be imported as a timer available implicitly.
Attributes
- Returns
-
a future that completes either with the result of the initial future, or with the result of the given computation in case of timeout
- Since
-
1.0
- Source
- timeouts.scala
Adds a orTimeout method similar to Java's java.util.concurrent.CompletableFuture#orTimeout.
Adds a orTimeout method similar to Java's java.util.concurrent.CompletableFuture#orTimeout.
The new future has the same outcome, unless the computation times out. In that case, it is a failed future with a java.util.concurrent.TimeoutException. An optional cancellation code can be passed by name.
Value parameters
- cancelCode
-
arbitrary code to run on timeout; this is run ''after'' the future is failed, on the execution context (not the timer); defaults to noOp.
- seconds
-
timeout, in seconds.
- timer
-
a timer used to cancel the future after the timeout, and to run the cancellation code, if any. For convenience, timeoutTimer can be imported as a timer available implicitly.
Attributes
- Returns
-
a future that completes either with the result of the initial future, or with a java.util.concurrent.TimeoutException.
- Since
-
1.0
- Source
- timeouts.scala
Same as standard await
but in seconds.
A single countdown
, followed by await
.
A single countdown
, followed by await
.
Same as standard tryAcquire
but in seconds.
Same as standard tryAcquire
but in seconds.
Attributes
- Since
-
1.1
- Note
-
Should be named
tryAcquire
, but that conflicts with existing signatures. - Source
- extensions.scala
Adds an isMarkedThread
method to threads.
Same as isSpinning(seconds = 1.0)
.
Same as isSpinning(seconds, threshold = 0.01)
.
Checks if a thread is spinning.
Checks if a thread is spinning.
This method works by calculating how much CPU time a thread is using during a span of time. It then returns true if this time is above a specified threshold. If the thread is not alive when the method starts or terminates while measuring CPU, the method returns false. The method also returns false for virtual threads.
Value parameters
- seconds
-
The span of time used to measure CPU activity; must be positive; defaults to 1 second.
- threshold
-
The threshold of activity to reach to be considered spinning; must be between 0 and 1.
Attributes
- Throws
-
UnsupportedOperationException
if measuring CPU time is not supported by the platform.
- Source
- extensions.scala
Waits for thread termination.
Waits for thread termination.
Value parameters
- seconds
-
timeout, in seconds.
- start
-
A starting point for wait time, as per getTime.
Attributes
- Returns
-
true is thread terminates within time limit.
- Source
- extensions.scala
Deprecated extensions
Like offer
but timeout in seconds.
Like poll
but timeout in seconds.
Attributes
- Deprecated
-
[Since version 1.3]
- Source
- extensions.scala