AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



programming-multithreading: WAP for Producer Consumer problem

Please give me code for Producer Consumer problem.

programming x 169
multithreading x 21
Posted On : 2016-05-04 22:45:29.0
profile Saksham Kumar - anyforum.in Saksham Kumar
73433932708
up-rate
4
down-rate

Answers


BlockingQueue implementation of Producer-Consumer design pattern by providing simplifies out of box support of blocking on put() and take(). we don´t need to write critical piece of wait-notify code to implement communication. BlockingQuue is an interface and Java 5 provides different implementation like ArrayBlockingQueue and LinkedBlockingQueue , both implement FIFO order or elements, while ArrayLinkedQueue is bounded in nature LinkedBlockingQueue is optionally bounded. below is a complete code example of Producer Consumer pattern with BlockingQueue. Compare it with classic wait notify code, its much simpler and easy to understand.


Blocking Queue to implement Producer Consumer Pattern:
-------------------------------------------------------------------------------------------------------------------

ProducerConsumerPattern.java:
------------------------------------------------------------------------------
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ProducerConsumerPattern {

public static void main(String args[]){

//Creating shared object
BlockingQueue sharedQueue = new LinkedBlockingQueue();

//Creating Producer and Consumer Thread
Thread prodThread = new Thread(new Producer(sharedQueue));
Thread consThread = new Thread(new Consumer(sharedQueue));

//Starting producer and Consumer thread
prodThread.start();
consThread.start();
}

}

Producer Class in java:
-----------------------------------------------------------

class Producer implements Runnable {

private final BlockingQueue sharedQueue;

public Producer(BlockingQueue sharedQueue) {
this.sharedQueue = sharedQueue;
}

@Override
public void run() {
for(int i=0; i<10; i++){
try {
System.out.println("Produced: " + i);
sharedQueue.put(i);
} catch (InterruptedException ex) {
Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

}

Consumer Class in Java:
--------------------------------------------------------------------

class Consumer implements Runnable{

private final BlockingQueue sharedQueue;

public Consumer (BlockingQueue sharedQueue) {
this.sharedQueue = sharedQueue;
}

@Override
public void run() {
while(true){
try {
System.out.println("Consumed: "+ sharedQueue.take());
} catch (InterruptedException ex) {
Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}


}

Output:
-------------------------------------
Produced: 0
Produced: 1
Consumed: 0
Produced: 2
Consumed: 1
Produced: 3
Consumed: 2
Produced: 4
Consumed: 3
Produced: 5
Consumed: 4
Produced: 6
Consumed: 5
Produced: 7
Consumed: 6
Produced: 8
Consumed: 7
Produced: 9
Consumed: 8
Consumed: 9

Posted On : 2016-05-04 23:01:56
Satisfied : 1 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523188241268
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in