Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

# ReadLock

```
open class ReadLock : Lock, Serializable
```

|---|---|
| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html) ||
| ↳ | [java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock](https://developer.android.com/reference/kotlin/java/util/concurrent/locks/ReentrantReadWriteLock.ReadLock#) |

The lock returned by method `https://developer.android.com/reference/kotlin/java/util/concurrent/locks/ReentrantReadWriteLock#readLock()`.

## Summary

| Protected constructors ||
|---|---|
| `https://developer.android.com/reference/kotlin/java/util/concurrent/locks/ReentrantReadWriteLock.ReadLock#ReadLock(java.util.concurrent.locks.ReentrantReadWriteLock)(lock: https://developer.android.com/reference/kotlin/java/util/concurrent/locks/ReentrantReadWriteLock!)` Constructor for use by subclasses. |

| Public methods ||
|---|---|
| open [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | `https://developer.android.com/reference/kotlin/java/util/concurrent/locks/ReentrantReadWriteLock.ReadLock#lock()()` Acquires the read lock. |
| open [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | `https://developer.android.com/reference/kotlin/java/util/concurrent/locks/ReentrantReadWriteLock.ReadLock#lockInterruptibly()()` Acquires the read lock unless the current thread is [interrupted](https://developer.android.com/reference/kotlin/java/lang/Thread.html#interrupt()). |
| open [Condition](https://developer.android.com/reference/kotlin/java/util/concurrent/locks/Condition)! | `https://developer.android.com/reference/kotlin/java/util/concurrent/locks/ReentrantReadWriteLock.ReadLock#newCondition()()` Throws `UnsupportedOperationException` because `ReadLocks` do not support conditions. |
| open [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html) | `https://developer.android.com/reference/kotlin/java/util/concurrent/locks/ReentrantReadWriteLock.ReadLock#toString()()` Returns a string identifying this lock, as well as its lock state. |
| open [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | `https://developer.android.com/reference/kotlin/java/util/concurrent/locks/ReentrantReadWriteLock.ReadLock#tryLock()()` Acquires the read lock only if the write lock is not held by another thread at the time of invocation. |
| open [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | `https://developer.android.com/reference/kotlin/java/util/concurrent/locks/ReentrantReadWriteLock.ReadLock#tryLock(kotlin.Long,%20java.util.concurrent.TimeUnit)(timeout: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-long/index.html, unit: https://developer.android.com/reference/kotlin/java/util/concurrent/TimeUnit.html#!)` Acquires the read lock if the write lock is not held by another thread within the given waiting time and the current thread has not been [interrupted](https://developer.android.com/reference/kotlin/java/lang/Thread.html#interrupt()). |
| open [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | `https://developer.android.com/reference/kotlin/java/util/concurrent/locks/ReentrantReadWriteLock.ReadLock#unlock()()` Attempts to release this lock. |

## Protected constructors

### ReadLock

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
protected ReadLock(lock: ReentrantReadWriteLock!)
```

Constructor for use by subclasses.

| Parameters ||
|---|---|
| `lock` | [ReentrantReadWriteLock](https://developer.android.com/reference/kotlin/java/util/concurrent/locks/ReentrantReadWriteLock)!: the outer lock object |

| Exceptions ||
|---|---|
| `java.lang.NullPointerException` | if the lock is null |

## Public methods

### lock

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
open fun lock(): Unit
```

Acquires the read lock.

Acquires the read lock if the write lock is not held by another thread and returns immediately.

If the write lock is held by another thread then the current thread becomes disabled for thread scheduling purposes and lies dormant until the read lock has been acquired.

### lockInterruptibly

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
open fun lockInterruptibly(): Unit
```

Acquires the read lock unless the current thread is [interrupted](https://developer.android.com/reference/kotlin/java/lang/Thread.html#interrupt()).

Acquires the read lock if the write lock is not held by another thread and returns immediately.

If the write lock is held by another thread then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happens:

- The read lock is acquired by the current thread; or
- Some other thread [interrupts](https://developer.android.com/reference/kotlin/java/lang/Thread.html#interrupt()) the current thread.

If the current thread:

- has its interrupted status set on entry to this method; or
- is [interrupted](https://developer.android.com/reference/kotlin/java/lang/Thread.html#interrupt()) while acquiring the read lock,

then `https://developer.android.com/reference/kotlin/java/lang/InterruptedException.html#` is thrown and the current thread's interrupted status is cleared.

In this implementation, as this method is an explicit interruption point, preference is given to responding to the interrupt over normal or reentrant acquisition of the lock.

| Exceptions ||
|---|---|
| `java.lang.InterruptedException` | if the current thread is interrupted |

### newCondition

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
open fun newCondition(): Condition!
```

Throws `UnsupportedOperationException` because `ReadLocks` do not support conditions.

| Return ||
|---|---|
| `https://developer.android.com/reference/kotlin/java/util/concurrent/locks/Condition!` | A new `https://developer.android.com/reference/kotlin/java/util/concurrent/locks/Condition` instance for this `Lock` instance |

| Exceptions ||
|---|---|
| `java.lang.UnsupportedOperationException` | always |

### toString

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
open fun toString(): String
```

Returns a string identifying this lock, as well as its lock state. The state, in brackets, includes the String `"Read locks ="` followed by the number of held read locks.

| Return ||
|---|---|
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html` | a string identifying this lock, as well as its lock state |

### tryLock

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
open fun tryLock(): Boolean
```

Acquires the read lock only if the write lock is not held by another thread at the time of invocation.

Acquires the read lock if the write lock is not held by another thread and returns immediately with the value `true`. Even when this lock has been set to use a fair ordering policy, a call to `tryLock()` *will* immediately acquire the read lock if it is available, whether or not other threads are currently waiting for the read lock. This "barging" behavior can be useful in certain circumstances, even though it breaks fairness. If you want to honor the fairness setting for this lock, then use `https://developer.android.com/reference/kotlin/java/util/concurrent/locks/ReentrantReadWriteLock.ReadLock#tryLock(kotlin.Long,%20java.util.concurrent.TimeUnit)` which is almost equivalent (it also detects interruption).

If the write lock is held by another thread then this method will return immediately with the value `false`.

| Return ||
|---|---|
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html` | `true` if the read lock was acquired |

### tryLock

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
open fun tryLock(
    timeout: Long, 
    unit: TimeUnit!
): Boolean
```

Acquires the read lock if the write lock is not held by another thread within the given waiting time and the current thread has not been [interrupted](https://developer.android.com/reference/kotlin/java/lang/Thread.html#interrupt()).

Acquires the read lock if the write lock is not held by another thread and returns immediately with the value `true`. If this lock has been set to use a fair ordering policy then an available lock *will not* be acquired if any other threads are waiting for the lock. This is in contrast to the `https://developer.android.com/reference/kotlin/java/util/concurrent/locks/ReentrantReadWriteLock.ReadLock#tryLock()` method. If you want a timed `tryLock` that does permit barging on a fair lock then combine the timed and un-timed forms together:

```kotlin
<code>if (lock.tryLock() ||
      lock.tryLock(timeout, unit)) {
    ...
  }</code>
```

If the write lock is held by another thread then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three things happens:

- The read lock is acquired by the current thread; or
- Some other thread [interrupts](https://developer.android.com/reference/kotlin/java/lang/Thread.html#interrupt()) the current thread; or
- The specified waiting time elapses.

If the read lock is acquired then the value `true` is returned.

If the current thread:

- has its interrupted status set on entry to this method; or
- is [interrupted](https://developer.android.com/reference/kotlin/java/lang/Thread.html#interrupt()) while acquiring the read lock,

then `https://developer.android.com/reference/kotlin/java/lang/InterruptedException.html#` is thrown and the current thread's interrupted status is cleared.

If the specified waiting time elapses then the value `false` is returned. If the time is less than or equal to zero, the method will not wait at all.

In this implementation, as this method is an explicit interruption point, preference is given to responding to the interrupt over normal or reentrant acquisition of the lock, and over reporting the elapse of the waiting time.

| Parameters ||
|---|---|
| `time` | the maximum time to wait for the lock |
| `unit` | [TimeUnit](https://developer.android.com/reference/kotlin/java/util/concurrent/TimeUnit.html#)!: the time unit of the timeout argument |
| `timeout` | [Long](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-long/index.html): the time to wait for the read lock |

| Return ||
|---|---|
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html` | `true` if the read lock was acquired |

| Exceptions ||
|---|---|
| `java.lang.InterruptedException` | if the current thread is interrupted |
| `java.lang.NullPointerException` | if the time unit is null |

### unlock

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
open fun unlock(): Unit
```

Attempts to release this lock.

If the number of readers is now zero then the lock is made available for write lock attempts. If the current thread does not hold this lock then `https://developer.android.com/reference/kotlin/java/lang/IllegalMonitorStateException.html#` is thrown.

| Exceptions ||
|---|---|
| `java.lang.IllegalMonitorStateException` | if the current thread does not hold this lock |