Consumers

Report a typo

Look at the code snippet below and pick the right option about the consumer threads.

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

fun addElements(queue: Queue<Int>) {
    for (i in 1..10) queue.add(i)
}

fun pollElements(queue: Queue<Int>) {
    while (true) {
        val number: Int? = queue.poll()
        if (number != null) println("${Thread.currentThread().name}: $number")
    }
}

fun main() {
    val queue = ConcurrentLinkedQueue<Int>()

    val generator = thread(start = false, name = "generator", block = {
        addElements(queue)
    })
    val poller1 = thread(start = false, name = "Thread-1", block = {
        pollElements(queue)
    })
    val poller2 = thread(start = false, name = "Thread-2", block = {
        pollElements(queue)
    })

    generator.start()
    poller1.start()
    poller2.start()
}
Select one option from the list
___

Create a free account to access the full topic