A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/lazy.html below:

Website Navigation


lazy

lazy

Creates a new instance of the Lazy that uses the specified initialization function initializer and the default thread-safety mode LazyThreadSafetyMode.SYNCHRONIZED. The lock used is both platform- and implementation- specific detail.

If the initialization of a value throws an exception, it will attempt to reinitialize the value at next access.

Since Kotlin1.0 Samples
import kotlin.concurrent.thread

fun main() { 
   //sampleStart 
   val answer: Int by lazy {
    println("Computing the answer to the Ultimate Question of Life, the Universe, and Everything")
    42
}

println("What is the answer?")
// Will print 'Computing...' and then 42
println(answer) // 42
println("Come again?")
// Will just print 42
println(answer) // 42 
   //sampleEnd
}

Creates a new instance of the Lazy that uses the specified initialization function initializer and thread-safety mode.

If the initialization of a value throws an exception, it will attempt to reinitialize the value at next access.

For LazyThreadSafetyMode.SYNCHRONIZED, the lock used is both platform- and implementation- specific detail.

Since Kotlin1.0 Samples
import kotlin.concurrent.thread

fun main() { 
   //sampleStart 
   val answer: Int by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
    println("Computing the answer to the Ultimate Question of Life, the Universe, and Everything")
    42
}

val t1 = thread {
    println("Thread 1: $answer")
}

val t2 = thread {
    println("Thread 2: $answer")
}

// It is guaranteed that 'Computing' message will be printed once, but both threads will see 42 as an answer
t1.join()
t2.join() 
   //sampleEnd
}
import kotlin.concurrent.thread

fun main() { 
   //sampleStart 
   class Answer(val value: Int, val computedBy: Thread = Thread.currentThread()) {
    override fun toString() = "Answer: $value, computed by: $computedBy"
}

val answer: Answer by lazy(LazyThreadSafetyMode.PUBLICATION) {
    println("Computing the answer to the Ultimate Question of Life, the Universe, and Everything")
    Answer(42)
}

val t1 = thread(name = "#1") {
    println("Thread 1: $answer")
}

val t2 = thread(name = "#2") {
    println("Thread 2: $answer")
}

// It is **not** guaranteed that 'Computing' message will be printed once,
// but guaranteed that both threads will see 42 computed by *the same* thread as an answer
t1.join()
t2.join() 
   //sampleEnd
}
Deprecated

Synchronization on Any? object is supported only in Kotlin/JVM.

Replace with

Creates a new instance of the Lazy that uses the specified initialization function initializer.

The lock parameter is ignored.

Since Kotlin1.0

Creates a new instance of the Lazy that uses the specified initialization function initializer.

Since Kotlin1.1

Creates a new instance of the Lazy that uses the specified initialization function initializer.

The mode parameter is ignored.

Since Kotlin1.1 Deprecated

Synchronization on Any? object is not supported.

Replace with

Creates a new instance of the Lazy that uses the specified initialization function initializer.

The lock parameter is ignored.

Since Kotlin1.1

Creates a new instance of the Lazy that uses the specified initialization function initializer and the default thread-safety mode LazyThreadSafetyMode.SYNCHRONIZED.

If the initialization of a value throws an exception, it will attempt to reinitialize the value at next access.

The returned instance uses itself to synchronize on. Do not synchronize from external code on the returned instance as it may cause accidental deadlock. This behavior might be changed in the future.

Since Kotlin1.0 Samples
import kotlin.concurrent.thread

fun main() { 
   //sampleStart 
   val answer: Int by lazy {
    println("Computing the answer to the Ultimate Question of Life, the Universe, and Everything")
    42
}

println("What is the answer?")
// Will print 'Computing...' and then 42
println(answer) // 42
println("Come again?")
// Will just print 42
println(answer) // 42 
   //sampleEnd
}

Creates a new instance of the Lazy that uses the specified initialization function initializer and thread-safety mode.

If the initialization of a value throws an exception, it will attempt to reinitialize the value at next access.

When the LazyThreadSafetyMode.SYNCHRONIZED mode is specified, the returned instance uses itself to synchronize on. Do not synchronize from external code on the returned instance as it may cause accidental deadlock. This behavior might be changed in the future.

Since Kotlin1.0 Samples
import kotlin.concurrent.thread

fun main() { 
   //sampleStart 
   val answer: Int by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
    println("Computing the answer to the Ultimate Question of Life, the Universe, and Everything")
    42
}

val t1 = thread {
    println("Thread 1: $answer")
}

val t2 = thread {
    println("Thread 2: $answer")
}

// It is guaranteed that 'Computing' message will be printed once, but both threads will see 42 as an answer
t1.join()
t2.join() 
   //sampleEnd
}
import kotlin.concurrent.thread

fun main() { 
   //sampleStart 
   class Answer(val value: Int, val computedBy: Thread = Thread.currentThread()) {
    override fun toString() = "Answer: $value, computed by: $computedBy"
}

val answer: Answer by lazy(LazyThreadSafetyMode.PUBLICATION) {
    println("Computing the answer to the Ultimate Question of Life, the Universe, and Everything")
    Answer(42)
}

val t1 = thread(name = "#1") {
    println("Thread 1: $answer")
}

val t2 = thread(name = "#2") {
    println("Thread 2: $answer")
}

// It is **not** guaranteed that 'Computing' message will be printed once,
// but guaranteed that both threads will see 42 computed by *the same* thread as an answer
t1.join()
t2.join() 
   //sampleEnd
}

Creates a new instance of the Lazy that uses the specified initialization function initializer and the default thread-safety mode LazyThreadSafetyMode.SYNCHRONIZED.

If the initialization of a value throws an exception, it will attempt to reinitialize the value at next access.

The returned instance uses the specified lock object to synchronize on. When the lock is not specified the instance uses itself to synchronize on, in this case do not synchronize from external code on the returned instance as it may cause accidental deadlock. This behavior might be changed in the future.

Since Kotlin1.0 Samples
import kotlin.concurrent.thread

fun main() { 
   //sampleStart 
   val lock = Any()
val answer: Int by lazy(lock) {
    println("Computing the answer to the Ultimate Question of Life, the Universe, and Everything")
    42
}

// Lock is acquired first, so thread cannot compute the answer
val thread: Thread
synchronized(lock) {
    thread = thread(name = "#1") {
        println("Thread is asking for an answer")
        println("$answer")
    }
    println("Let's hold the thread #1 for a while with a lock")
    Thread.sleep(100) // Let it wait
}
// Lock is unlocked, the thread will print an answer
thread.join() 
   //sampleEnd
}

Creates a new instance of the Lazy that uses the specified initialization function initializer and the default thread-safety mode LazyThreadSafetyMode.SYNCHRONIZED.

If the initialization of a value throws an exception, it will attempt to reinitialize the value at next access.

Note that the returned instance uses itself to synchronize on. Do not synchronize from external code on the returned instance as it may cause accidental deadlock. This behavior might be changed in the future.

Since Kotlin1.3 Samples
import kotlin.concurrent.thread

fun main() { 
   //sampleStart 
   val answer: Int by lazy {
    println("Computing the answer to the Ultimate Question of Life, the Universe, and Everything")
    42
}

println("What is the answer?")
// Will print 'Computing...' and then 42
println(answer) // 42
println("Come again?")
// Will just print 42
println(answer) // 42 
   //sampleEnd
}

Creates a new instance of the Lazy that uses the specified initialization function initializer and thread-safety mode.

If the initialization of a value throws an exception, it will attempt to reinitialize the value at next access.

Since Kotlin1.3 Samples
import kotlin.concurrent.thread

fun main() { 
   //sampleStart 
   val answer: Int by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
    println("Computing the answer to the Ultimate Question of Life, the Universe, and Everything")
    42
}

val t1 = thread {
    println("Thread 1: $answer")
}

val t2 = thread {
    println("Thread 2: $answer")
}

// It is guaranteed that 'Computing' message will be printed once, but both threads will see 42 as an answer
t1.join()
t2.join() 
   //sampleEnd
}
import kotlin.concurrent.thread

fun main() { 
   //sampleStart 
   class Answer(val value: Int, val computedBy: Thread = Thread.currentThread()) {
    override fun toString() = "Answer: $value, computed by: $computedBy"
}

val answer: Answer by lazy(LazyThreadSafetyMode.PUBLICATION) {
    println("Computing the answer to the Ultimate Question of Life, the Universe, and Everything")
    Answer(42)
}

val t1 = thread(name = "#1") {
    println("Thread 1: $answer")
}

val t2 = thread(name = "#2") {
    println("Thread 2: $answer")
}

// It is **not** guaranteed that 'Computing' message will be printed once,
// but guaranteed that both threads will see 42 computed by *the same* thread as an answer
t1.join()
t2.join() 
   //sampleEnd
}
Deprecated

Synchronization on Any? object is not supported.

Replace with

Creates a new instance of the Lazy that uses the specified initialization function initializer and the default thread-safety mode LazyThreadSafetyMode.SYNCHRONIZED.

If the initialization of a value throws an exception, it will attempt to reinitialize the value at next access.

The returned instance uses the specified lock object to synchronize on.

Since Kotlin1.3

Creates a new instance of the Lazy that uses the specified initialization function initializer.

Since Kotlin1.8

Creates a new instance of the Lazy that uses the specified initialization function initializer.

The mode parameter is ignored.

Since Kotlin1.8 Deprecated

Synchronization on Any? object is not supported.

Replace with

Creates a new instance of the Lazy that uses the specified initialization function initializer.

The lock parameter is ignored.

Since Kotlin1.8

Creates a new instance of the Lazy that uses the specified initialization function initializer.

Since Kotlin1.8

Creates a new instance of the Lazy that uses the specified initialization function initializer.

The mode parameter is ignored.

Since Kotlin1.8 Deprecated

Synchronization on Any? object is not supported.

Replace with

Creates a new instance of the Lazy that uses the specified initialization function initializer.

The lock parameter is ignored.

Since Kotlin1.8

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4