Menu
Topics Index
...
`

Threads1946


If you need explanation Read this topic

If you need Answer Take test on this topic

Threads1946
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);
            }
        }
    }
}

A. 6
B. 001122
C. 012012
D. Some other output
E. Output can not be determined
F. Compilation Error or Runtime Error
Topic: Other Advanced Topics In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

e is the ans we get out of memory exception since the heap can't hold the data we are inserting into

Posted by Karteek Paruchuri    2013-11-18 12:35:57


B .012012 is the answer. as satisfying for loop first 0 next 1 , 2 and then again satisfying for loop 0 1 2 gets printed...

Posted by Shabaaz Shaikz    2013-11-18 12:50:25


ans must be C.. we can call run any no of tyms but start must be called only once.. as we call run firstly the thread run method gets executed and 012 gets printed.. and nw call is made to strt so again 012 gets printed.. bt if strt would hav been called bfore run then the output would hav been undetrmined

Posted by Pramod Jain    2013-11-19 05:32:06


Congratulations Pramod Jain. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2013-11-20 03:58:07


oh..i taught 012012 is on option B. :-)

Posted by Shabaaz Shaikz    2013-11-20 07:18:43


thank u mc :) got rc :) :)

Posted by Pramod Jain    2013-12-03 04:35:26


© meritcampus 2019

All Rights Reserved.

Open In App