Application

Report a typo

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

public class GeneratorAndConsumers {
    public static void main(String[] args) {
        Queue<Integer> queue = new ConcurrentLinkedQueue<>();

        Thread generator = new Thread(() -> addElements(queue));
        Thread poller1 = new Thread(() -> pollElements(queue)); // Thread-1
        Thread poller2 = new Thread(() -> pollElements(queue)); // Thread-2

        generator.start();
        poller1.start();
        poller2.start();
    }

    private static void addElements(Queue<Integer> queue) {
        for (int i = 1; i <= 10; i++) {
            queue.add(i);
        }
    }

    private static void pollElements(Queue<Integer> queue) {
        while (true) {
            Integer number = queue.poll();
            if (number != null) {
                System.out.println(Thread.currentThread().getName() + ": " + number);
            }
        }
    }
}
Select one option from the list
___

Create a free account to access the full topic