A conflict of 3 threads

Report a typo

Choose the right collections in the createSynchronizedList method:

import java.util.*
import kotlin.concurrent.thread

// choose the collection
fun createSynchronizedList(): MutableList<Int> {
    return ...
}

fun addNumbers(numbers: MutableList<Int>) {
    for (i in 0 until 100_000) numbers.add(i)
}

fun main() {
    val numbers: MutableList<Int> = createSynchronizedList()
    val thread1 = thread(start = false, name = "Thread 1", block = {
        addNumbers(numbers)
    })
    val thread2 = thread(start = false, name = "Thread 2", block = {
        addNumbers(numbers)
    })

    thread1.start()
    thread2.start()
    addNumbers(numbers)

    thread1.join()
    thread2.join()

    println(numbers.size) // 300_000
}
Select one or more options from the list
___

Create a free account to access the full topic