Producer Consumer Problem - JAVA

//PRODUCER CONSUMER PROBLEM
class Buffer
{
private int data;
private boolean available=false;
public synchronized int get()
{
while(available==false)
{
try
{
wait();
}
catch ( InterruptedException e)
{}
}
available=false;
notifyAll();
return data;
}
public synchronized void put(int value )
{
while(available==true)
{
try
{
wait();
}
catch ( InterruptedException e)
{}
}
data=value;
available=true;
notifyAll();
}
}
class Producer extends Thread
{
private Buffer buf;
public Producer (Buffer c)
{
buf=c;
}
public void run()
{
for(int i=0;i<5;i++)
{
buf.put(i);
System.out.println("Producer produced item:"+i);
try
{
sleep( (int) (Math.random()*100));
}
catch (InterruptedException e) { }
}
}
}
class Consumer extends Thread
{
private Buffer buf;
public Consumer (Buffer c)
{
buf=c;
}
public void run()
{
int value=0;
for(int i=0;i<5;i++)
{
value=buf.get();
System.out.println("Consumer consumed item:"+value);
try
{
sleep( (int) (Math.random()*100));
}
catch (InterruptedException e) { }
}
}
}
public class prodcons
{
public static void main (String args[])
{
Buffer b=new Buffer();
Producer p=new Producer(b);
Consumer c=new Consumer(b);
p.start();
c.start();
}
}
OUTPUT
students@ccflab-desktop:~$ javac prodcons.java
students@ccflab-desktop:~$ java prodcons
Producer produced item:0
Consumer consumed item:0
Producer produced item:1
Consumer consumed item:1
Producer produced item:2
Consumer consumed item:2
Producer produced item:3
Consumer consumed item:3
Producer produced item:4                                      //http://2k8618.blogspot.com
Consumer consumed item:4
students@ccflab-desktop:~$

Download

26 comments:

  1. The simplest way to solve the producer consumer problem is by using Blocking-queue, as explained in this tutorial, but if you really want to understand the core concept behind producer consumer pattern i.e. inter-thread communication in Java, then you must solve this problem using wait and notify method in Java. when you solve it hard way, you learn more.

    ReplyDelete
  2. I was a girl, but clumsy things. I do not know how to cook, sew, above, ca. I have too insipid and tedious, but that's my personality. It's hard to change
    baixar facebook l baixar facebook l baixar whatsapp l baixar whatsapp

    ReplyDelete
  3. I like your post. It really useful with me. Thanks for sharing these useful information!
    happywheels3game.com

    ReplyDelete
  4. I can feel that the articles contained in this blog is so interesting. I also get a variety informasin, thanks. geometry-dash.net

    ReplyDelete
  5. I like your post. It really useful with me. Thanks for sharing these useful information!
    papasgamesonline.com

    ReplyDelete
  6. This article is really fantastic and thanks for sharing the valuable post

    overwatch shirts

    ReplyDelete
  7. I was very impressed by this post, this site has always been pleasant news. Thank you very much for such an interesting post. Keep working, great job! In my free time, I like play game: facebooklite.com.br. What about you?

    ReplyDelete
  8. This is a great article. It gave me a lot of useful information. thank you very much.
    sushi-cat2.com

    ReplyDelete
  9. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. www.facebookbaixar.com.br

    ReplyDelete
  10. I really like your blog! Continue to write more! Very interesting! swordsandsouls.net

    ReplyDelete
  11. I enjoyed over read your blog post. Your blog have nice information, I got good ideas from this amazing blog. I am always searching like this type blog post. I hope I will see again. fireboy-watergirl.com

    ReplyDelete
  12. Welcome, your blog is great. It's very interesting and meaningful, it helps me have more knowledge about life. dr-driving.com

    ReplyDelete
  13. Thank you very much for the information you shared, it’s all I’ve been looking for
    www.happywheels-2.com

    ReplyDelete
  14. Alot of blogs I see these days don't really provide anything that I'm interested in, but I'm most definately interested in this one. Just thought that I would post and let you know. Nice! thank you so much! Thank you for sharing.
    b612az.com

    ReplyDelete
  15. Thank you, so Much admin have a nice day :) you information about weight loss is very easy and simple.
    pocafashion

    ReplyDelete
  16. I appreciate you and hopping for some more informative posts. thanks for share
    run3.run

    ReplyDelete
  17. I am looking for my memories through the stories, the narrative of people. I feel it is difficult but I will try.
    povaup

    ReplyDelete
  18. Your article is very interesting. I think this article has a lot of information needed, looking forward to your new posts. Get permission to share:
    returnman3game.com

    ReplyDelete
  19. Your article has a lot of good information. It proves you understand very well about this issue, I agree completely on your article.
    http://carriagereviews.com/best-fuel-injector-cleaner-reviews-and-comparison

    ReplyDelete
  20. I have read your article, the information you give is very interesting.
    pocatravel
    I have read your article, the information you give is very interesting.
    pocatravel

    ReplyDelete
  21. Your post is interesting. Hope you have more useful posts like this
    vex3.games

    ReplyDelete
  22. I was working and suddenly I visits your site frequently and recommended it to me to read also. The writing style is superior and the content is relevant. Thanks for the insight you provide the readers!
    instagram viewer images

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...