tinyscalautils.threads

Members list

Type members

Classlikes

object DelayedFuture

Implicit use of timers.

Implicit use of timers.

Attributes

Source
Timer.scala
Supertypes
class Object
trait Matchable
class Any
Self type
object Execute

Attributes

Source
Executors.scala
Supertypes
class Object
trait Matchable
class Any
Self type
Execute.type
object ExecuteAfter

Attributes

Source
Executors.scala
Supertypes
class Object
trait Matchable
class Any
Self type
class Executors

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
class Object
trait Matchable
class Any
Known subtypes
object Executors
object Executors extends 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
class Executors
class Object
trait Matchable
class Any
Self type
Executors.type
final class KeepThreadsFactory extends ThreadFactory

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
class Object
trait Matchable
class Any

Factory methods.

Factory methods.

Attributes

Since

1.0

Companion
class
Source
KeepThreadsFactory.scala
Supertypes
class Object
trait Matchable
class Any
Self type
trait MarkedThread extends Thread

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
class Thread
trait Runnable
class Object
trait Matchable
class Any

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
class Object
trait Matchable
class Any
Known subtypes

A default marked thread factory.

A default marked thread factory.

Attributes

Companion
class
Source
MarkedThreadFactory.scala
Supertypes
class Object
trait Matchable
class Any
Self type
object Run

Attributes

Source
Executors.scala
Supertypes
class Object
trait Matchable
class Any
Self type
Run.type
object RunAfter

Attributes

Source
Executors.scala
Supertypes
class Object
trait Matchable
class Any
Self type
RunAfter.type
trait Timer extends AutoCloseable

Simple timers.

Simple timers.

Attributes

Since

1.0

Source
Timer.scala
Supertypes
class Object
trait Matchable
class Any

Value members

Concrete methods

def newThread[U](name: String, start: Boolean, daemon: Boolean, waitForChildren: Boolean)(code: => U): Thread

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
def newThread[U](code: => U): Thread

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
def runAsync[A](code: => A)(using exec: Executor | ExecutionContext): A

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
transparent inline def withThreads[Exec, A](executor: Exec, inline shutdown: Boolean)(code: Exec ?=> A): Any

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
transparent inline def withThreads[Exec, A](executor: Exec)(code: Exec ?=> A): Any

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
transparent inline def withThreads[A](maxThreads: Int, awaitTermination: Boolean)(code: ExecutionContextExecutorService ?=> A): Any

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
transparent inline def withThreads[A](maxThreads: Int)(code: ExecutionContextExecutorService ?=> A): Any

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
transparent inline def withThreads[A](awaitTermination: Boolean)(code: ExecutionContextExecutorService ?=> A): Any

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
transparent inline def withThreads[A]()(code: ExecutionContextExecutorService ?=> A): Any

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

inline def withThreadPoolAndWait[A, Exec](exec: Exec, inline shutdown: Boolean)(code: Exec ?=> Future[A]): A

Attributes

Deprecated
[Since version 1.6]
Source
withThreads.scala
def withThreadsAndWait[A](maxThreads: Int, awaitTermination: Boolean)(code: ExecutionContextExecutorService ?=> Future[A]): A

Attributes

Deprecated
[Since version 1.6]
Source
withThreads.scala
def withUnlimitedThreads[U](awaitTermination: Boolean)(code: ExecutionContextExecutorService ?=> U): Unit

Attributes

Deprecated
[Since version 1.6]
Source
withThreads.scala

Attributes

Deprecated
[Since version 1.6]
Source
withThreads.scala
def withUnlimitedThreadsAndWait[A](awaitTermination: Boolean)(code: ExecutionContextExecutorService ?=> Future[A]): A

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

extension (barrier: CyclicBarrier)
def await(seconds: Double): Int

Same as standard await but in seconds.

Same as standard await but in seconds.

Attributes

Since

1.2

Source
extensions.scala
extension (exec: ExecutorService)

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
def shutdownAndWait(seconds: Double, force: Boolean): Boolean

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
extension (exec: Executor | ExecutionContext)
inline def run[U](inline code: U): Unit

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 type Runnable or Callable because it's probably a mistake: exec.run(code) was written when exec.execute(code) or exec.run(code.run()) or exec.run(code.call()) was intended. In the unlikely case that exec.run(code) was desired, add a type ascription: exec.run(code: AnyRef).

Source
Executors.scala
extension [A](future: Future[A])
def completeOnTimeout(seconds: Double, strict: Boolean)(fallbackCode: => A)(using exec: ExecutionContext, timer: Timer): Future[A]

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
def orTimeout(seconds: Double, cancelCode: => Any)(using exec: ExecutionContext, timer: Timer): Future[A]

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
extension (latch: CountDownLatch)
def await(seconds: Double): Boolean

Same as standard await but in seconds.

Same as standard await but in seconds.

Attributes

Since

1.1

Source
extensions.scala

A single countdown, followed by await.

A single countdown, followed by await.

Attributes

Source
extensions.scala

A single countdown, followed by await.

A single countdown, followed by await.

Attributes

Source
extensions.scala
extension (sem: Semaphore)
def acquire(permits: Int, seconds: Double): Boolean

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
extension (thread: Thread)

Adds an isMarkedThread method to threads.

Adds an isMarkedThread method to threads.

Attributes

See also
Since

1.0

Source
MarkedThread.scala
extension (thread: Thread)

Same as isSpinning(seconds = 1.0).

Same as isSpinning(seconds = 1.0).

Attributes

Source
extensions.scala
def isSpinning(seconds: Double): Boolean

Same as isSpinning(seconds, threshold = 0.01).

Same as isSpinning(seconds, threshold = 0.01).

Attributes

Source
extensions.scala
def isSpinning(seconds: Double, threshold: Double): Boolean

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
def joined(seconds: Double, start: Long): Boolean

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

extension [A](queue: BlockingQueue[A])
def offer(value: A, seconds: Double): Boolean

Like offer but timeout in seconds.

Like offer but timeout in seconds.

Attributes

Since

1.2

Source
extensions.scala
def poll(seconds: Double): A

Like poll but timeout in seconds.

Like poll but timeout in seconds.

Attributes

Since

1.3

Source
extensions.scala
def pollOption(seconds: Double): Option[A]

Attributes

Deprecated
[Since version 1.3]
Source
extensions.scala