Menu
Question Index
...

What will be the output of the following program?

import java.util.concurrent.*;
import java.util.logging.*;

public class ProducerConsumerPattern {
    public static void main(String args[]) {
        BlockingQueue sharedQueue = new LinkedBlockingQueue();
        Thread prodThread = new Thread(new Producer(sharedQueue));
        prodThread.run();
        prodThread.start();
    }
}
class Producer implements Runnable {
    private final BlockingQueue sharedQueue;
    public Producer(BlockingQueue sharedQueue) {
        this.sharedQueue = sharedQueue;
    }
    public void run() {
        for (int i = 0; i < 3; i++) {
            try {
                System.out.print(+i);
                sharedQueue.put(i);
            } catch (InterruptedException ex) {
                Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}


6
001122
012012
Some other output
Output can not be determined
Compilation Error or Runtime Error

Doubts

Problems

Topic: Other Advanced Topics In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App