Adding and polling

Report a typo

Take a look at the code snippet below and pick the right option about the numbers' size.

import java.util.Queue
import java.util.concurrent.ConcurrentLinkedQueue
import kotlin.concurrent.thread

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

fun pollNumbers(target: Queue<Int>) {
    for (i in 0 until 50_000) target.poll()
}

fun main() {
    val numbers = ConcurrentLinkedQueue<Int>()
    addNumbers(numbers)

    val thread1 = thread(start = false, name = "Thread 1", block = {
        pollNumbers(numbers)
    })

    val thread2 = thread(start = false, name = "Thread 2", block = {
        pollNumbers(numbers)
    })

    thread1.start()
    thread2.start()

    thread1.join()
    thread2.join()

    println(numbers.size)
}
Select one option from the list
___

Create a free account to access the full topic