com.r3corda.core / ThreadBox

# ThreadBox

`class ThreadBox<out T>`

A threadbox is a simple utility that makes it harder to forget to take a lock before accessing some shared state. Simply define a private class to hold the data that must be grouped under the same lock, and then pass the only instance to the ThreadBox constructor. You can now use the locked method with a lambda to take the lock in a way that ensures itll be released if theres an exception.

Note that this technique is not infallible: if you capture a reference to the fields in another lambda which then gets stored and invoked later, there may still be unsafe multi-threaded access going on, so watch out for that. This is just a simple guard rail that makes it harder to slip up.

Example:

private class MutableState { var i = 5 } private val state = ThreadBox(MutableState())

val ii = state.locked { i }

### Constructors

<init> | `ThreadBox(content: T, lock: ReentrantLock = ReentrantLock())` A threadbox is a simple utility that makes it harder to forget to take a lock before accessing some shared state. Simply define a private class to hold the data that must be grouped under the same lock, and then pass the only instance to the ThreadBox constructor. You can now use the locked method with a lambda to take the lock in a way that ensures itll be released if theres an exception.

### Properties

content | `val content: T`
lock | `val lock: ReentrantLock`

### Functions

alreadyLocked | `fun <R> alreadyLocked(body: T.() -> R): R`
checkNotLocked | `fun checkNotLocked(): <ERROR CLASS>`
locked | `fun <R> locked(body: T.() -> R): R`