## 10.5 Threads

The module `Thread` provides operations on first class threads.

Threads may be in one of three states, namely runnable, blocked, or terminated. Orthogonally, a thread may be suspended.

Runnable and non-suspended threads are scheduled according to their priorities, which may be `low`, `medium`, or `high`. The default priority is `medium`. The priority of a thread may influence its time share for execution, where threads with `medium` priority obtain at least as long a time share as threads with `low` priority and at most as long as threads with `high` priority. Implementations may also choose not to schedule a thread at all if a thread with higher priority is runnable.

A newly created thread inherits its priority from its parent if the latter has either `medium` or `low` priority. Otherwise, the new thread gets default (i. e., `medium`) priority.

`IsThread`

  > `{Thread.is` `+X``` `?B``}`

  test whether `X` is a thread.

`this`

  > `{Thread.this` `?Thread``}`

  returns the current thread.

`state`

  > `{Thread.state` `+Thread``` `?A``}`

  returns one of the atoms `runnable`, `blocked`, `terminated` according to the current state of `Thread`.

`resume`

  > `{Thread.resume` `+Thread``}`

  resumes `Thread`. Resumption undoes suspension.

`suspend`

  > `{Thread.suspend` `+Thread``}`

  suspends `Thread` such that it cannot be further reduced.

`isSuspended`

  > `{Thread.isSuspended` `+Thread``` `?B``}`

  tests whether `Thread` is currently suspended.

`injectException`

  > `{Thread.injectException` `+Thread``` `+X``}`

  raises `X` as exception on `Thread`. If `Thread` is terminated, an error exception is raised in the current thread.

`terminate`

  > `{Thread.terminate` `+Thread``}`

  raises an exception `kernel(terminate ...)` on `Thread`.

`getPriority`

  > `{Thread.getPriority` `+Thread``` `?A``}`

  returns one of them atoms `low`, `medium`, or `high` according to the current priority of `Thread`.

`setPriority`

  > `{Thread.setPriority` `+Thread``` `+A``}`

  sets priority of thread `Thread` to the priority described by atom `A`. `A` must be one of `low`, `medium`, or `high`.

`getThisPriority`

  > `{Thread.getThisPriority` `?A``}`

  returns one of them atoms `low`, `medium`, or `high` according to the priority of the current thread.

`setThisPriority`

  > `{Thread.setThisPriority` `+A``}`

  sets priority of the current thread to the priority described by atom `A`. `A` must be one of `low`, `medium`, or `high`.

`preempt`

  > `{Thread.preempt` `+Thread``}`

  preempts the current thread, i. e., immediately schedules another runnable thread (if there is one). `Thread` stays runnable.