Solving a Producer and Consumer problem in a multithreading environment following illustration is sited, I have tried to solve the problem using a Queue, Producer producing the counter and Consumer consumes the counter :
import java.util.LinkedList;
import java.util.Queue;
/**
* @author sachin.srivastava
* tek9g.blogspot.com
*
*/
public class ProducerConsumer extends Thread{
static Queue<Integer> queue = new LinkedList<>();
private static final int capacity = 10;
private static int counter = 1;
public static void main(String[] args) {
///starting the producer and consumer
new Producer().start();
new Consumer().start();
}
static class Producer extends Thread {
public void run(){
for ( ;;) {
try {
Thread.sleep(2000);
synchronized (queue) {
if( queue.size() <= capacity){
queue.add(counter);
System.out.println("queue added counter produced=" + counter);
counter++;
} else if(queue.size() > capacity) {
queue.remove(queue.size()-1);
System.out.println("queue is full so can't produce new element");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
static class Consumer extends Thread {
public void run(){
for (;;) {
try {
Thread.sleep(2000);
synchronized (queue) {
if(!queue.isEmpty()){
Integer head = queue.remove();
System.out.println("queue pop counter consumer=" + head);
} else {
System.out.println("queue is empty so nothing to pop for consumers");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
Output :
queue added counter produced=1
queue pop counter consumer=1
queue added counter produced=2
queue pop counter consumer=2
queue added counter produced=3
queue pop counter consumer=3
queue added counter produced=4
queue pop counter consumer=4
queue added counter produced=5
queue pop counter consumer=5
queue added counter produced=6
queue pop counter consumer=6
queue added counter produced=7
queue pop counter consumer=7
queue added counter produced=8
queue pop counter consumer=8
queue added counter produced=9
queue pop counter consumer=9
queue added counter produced=10
queue pop counter consumer=10
queue added counter produced=11
queue pop counter consumer=11
queue added counter produced=12
queue pop counter consumer=12
queue added counter produced=13
queue pop counter consumer=13
queue added counter produced=14
queue pop counter consumer=14
queue added counter produced=15
queue pop counter consumer=15
queue added counter produced=16
queue pop counter consumer=16
queue added counter produced=17
queue pop counter consumer=17
queue added counter produced=18
queue pop counter consumer=18
queue added counter produced=19
queue pop counter consumer=19
queue added counter produced=20
queue pop counter consumer=20
queue added counter produced=21
queue pop counter consumer=21
queue added counter produced=22
queue pop counter consumer=22
queue added counter produced=23
queue pop counter consumer=23
queue added counter produced=24
queue pop counter consumer=24
queue added counter produced=25
queue pop counter consumer=25
queue added counter produced=26
queue pop counter consumer=26
queue added counter produced=27
queue pop counter consumer=27
queue added counter produced=28
queue pop counter consumer=28
queue added counter produced=29
queue pop counter consumer=29
--------
If you seen the output starts with the producer 1 then consumer 1 then producer 2 then consumer 2 and so on ... so the queue always remain in the consistent state and also remain thread safe.
Enjoy the coding !!!!
No comments:
Post a Comment