Java: Producer Consumer Example – Handle Concurrent Read/Write

The producer-consumer problem (also known as the bounded-buffer problem) is a classic Java Example of a multi-process synchronization problem.

The problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue. The producer’s job is to generate a piece of data, put it into the buffer and start again.

At the same time, the consumer is consuming the data (i.e., removing it from the buffer) one piece at a time. The problem is to make sure that the producer won’t try to add data into the buffer if it’s full and that the consumer won’t try to remove data from an empty buffer.

Below simple Java Program will create this problem:

Java Examples – Producer Consumer Problem

CrunchifyProducerConsumer.java

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

packagecom.crunchify.tutorials;

importjava.util.Vector;

importjava.util.Iterator;

/**

* @author Crunchify

*/

publicclassCrunchifyProducerConsumer{

privatestaticVector<Object>data=newVector<Object>();

publicstaticvoidmain(String[]args){

newProducer().start();

newConsumer().start();

}

publicstaticclassConsumerextendsThread{

Consumer(){

super("Consumer");

}

@Override

publicvoidrun(){

for(;;){

try{

Thread.sleep(1);

}catch(Exceptione){

e.printStackTrace();

}

@SuppressWarnings("rawtypes")

Iterator it=data.iterator();

while(it.hasNext())

it.next();

}

}

}

publicstaticclassProducerextendsThread{

Producer(){

super("Producer");

}

@Override

publicvoidrun(){

for(;;){

try{

Thread.sleep(1);

}catch(Exceptione){

e.printStackTrace();

}

data.add(newObject());

if(data.size()>1000)

data.remove(data.size()-1);

}

}

}

}

Issue:

ConcurrentModificationException

1

2

3

4

Exception inthread"Consumer"java.util.ConcurrentModificationException

at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:819)

at java.util.ArrayList$Itr.next(ArrayList.java:791)

at com.crunchify.tutorials.CrunchifyProducerConsumer$Consumer.run(CrunchifyProducerConsumer.java:36)

Resolution:

Add the keyword synchronized to put a lock on the data while we are using it.

Better Java Program:

CrunchifyProducerConsumer.java

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

packagecrunchify.com.tutorials;

importjava.util.Iterator;

importjava.util.Vector;

/**

* @author Crunchify.com

* Producer Consumer Example in Java

*/

publicclassCrunchifyProducerConsumer{

privatestaticVector<Object>data=newVector<Object>();

publicstaticvoidmain(String[]args){

newProducer().start();

newConsumer().start();

}

publicstaticclassConsumerextendsThread{

Consumer(){

super("Consumer");

}

@SuppressWarnings("rawtypes")

@Override

publicvoidrun(){

for(;;){

try{

Thread.sleep(1000);

System.out.println("Object Consumed ################");

}catch(Exceptione){

e.printStackTrace();

}

synchronized(data){

Iterator it=data.iterator();

while(it.hasNext())

it.next();

}

}

}

}

publicstaticclassProducerextendsThread{

Producer(){

super("Producer");

}

@Override

publicvoidrun(){

for(;;){

try{

Thread.sleep(1000);

System.out.println("Object Produced ~~~~~~~~~~~~~~~");

}catch(Exceptione){

e.printStackTrace();

}

data.add(newObject());

if(data.size()>1000)

data.remove(data.size()-1);

}

}

}

}

Java Producer Consumer Example Result:

Have a suggestion on article? Please chime in and share it as a comment.

Subscribe to our newsletter and never miss a post!

Get early access to new articles, plugins, discount codes and brief updates about what's new with Crunchify! Join over 14 million monthly readers...

Enter your email address...

About App Shah

Hello & Good Day from greater New York. I'm an Engineer by profession, Blogger by passion & Founder of Crunchify, the largest free blogging & technical resource site for beginners. Love SEO, SaaS, #webperf, WordPress, Java. With more than 14 millions pageviews / month, Crunchify LLC, has changed the life of over thousands of individual around the globe teaching Java & Web technology without spending a money online. Get latest update on and .