write a program to implement the producer and consumer problem

write a program to implement the producer and consumer problem

write a program to implement the producer and consumer problem.

View Answers

May 8, 2012 at 11:22 AM

import java.util.*;
class Multithreading {
int val;
boolean value = false;
synchronized int get() {
if(!value)
try {
wait();
} catch(Exception e) {
System.out.println(e);
}
System.out.println("Get: " + val);
value = false;
notify();
return val;
}
synchronized void put(int val) {
if(value)
try {
wait();
}catch(Exception e) {
System.out.println(e);
}
this.val = val;
value = true;
System.out.println("Put: " + val);
notify();
}
}
class Producer implements Runnable {
Multithreading th;
Producer(Multithreading th) {
this.th = th;
new Thread(this, "Producer").start();
}
public void run() {
int i = 0;
while(i<=50) {
th.put(i++);
}
}
}

class Consumer implements Runnable {
Multithreading th;
Consumer(Multithreading th) {
this.th = th;
new Thread(this, "Consumer").start();
}
public void run() {
while(true) {
th.get();
}
}
}
class ProducerConsumer {
public static void main(String args[]) {
Multithreading th = new Multithreading();
new Producer(th);
new Consumer(th);
}
}

May 8, 2012 at 1:10 PM

In the above code, one thread is producing data and another is consuming it. The two threads "Producer" and "Consumer" share the synchronized methods of the class "Multithreading". At time of program execution, the "put( )" method is invoked through the "Producer" class which increments the variable "num" by 1. After producing 1 by the producer, the method "get( )" is invoked by through the "Consumer" class which retrieves the produced number and returns it to the output.









Related Tutorials/Questions & Answers:
producer and concumer problem
producer and concumer problem  Write aprogram by using the implementation of the Producer/Consumer problem. the programe ask user to write array... press stop and press start again the program produce adifferent value 1 0 0 1 0
ModuleNotFoundError: No module named 'Producer-Consumer'
ModuleNotFoundError: No module named 'Producer-Consumer'  Hi, My... named 'Producer-Consumer' How to remove the ModuleNotFoundError: No module named 'Producer-Consumer' error? Thanks   Hi, In your
Advertisements
Please can I get the code for solution of producer consumer problem using semaphores?
Please can I get the code for solution of producer consumer problem using semaphores?  Please can I get the code for solution of producer consumer problem using semaphores?   import java.util.*; class Semaphore
write a program that Implement an integrated COM client server to add two numbers.by visuall studio 2010
write a program that Implement an integrated COM client server to add two numbers.by visuall studio 2010  write a program that Implement an integrated COM client server to add two numbers.by visuall studio 2010 if someone can do
plz help me out with this problem...please write the program for me.thanxxxxx
plz help me out with this problem...please write the program for me.thanxxxxx  Write a program that will print out a hydro bill. The bill will be formatted as the example on the next page. The following information
plz help me out with this problem...please write the program for me.thanxxxxx
plz help me out with this problem...please write the program for me.thanxxxxx  Write a program that will print out a hydro bill. The bill will be formatted as the example on the next page. The following information
producer and concumer Interface program
producer and concumer Interface program  I need producer and concumer Interface program   Hi Friend, Try the following code:ADS...; System.out.println("Put: " + val); notify(); } } class Producer implements Runnable
write a program
write a program  write a program add 2 no.s without use arithmetic,unary operaters in java
Program to implement array of Objects.
Program to implement array of Objects.  Create a class it has following data members:- -name -account id -balance initialize the data members through constructors & define a print function & also define a function
program to implement array of objects
program to implement array of objects  import java.io.*; import java.util.*; public class dataa { DataInputStream in=new DataInputStream(System.in); int i; int a[i]=int acid[5]; String name1[i]=new String[5
write a javascript progam to implement functions in html
write a javascript progam to implement functions in html  write a javascript progam to implement functions in html  Please visit the following link: http://www.roseindia.net/javascript/emailvalidation.shtml http
write a program - Java Beginners
write a program  1. write a program that reads a string composed of 6 words then generate and print a word composed of the first letters of the 6 words.  Hi friend, Code to solve the problem : class FirstLetter
write a java program
write a java program  write a program to print '*' in a circular form
How to write Java Program
How to write Java Program  how to write a program to find average of 5 student marks
WRITE A PROGRAM IN JAVA
WRITE A PROGRAM IN JAVA  Write program which accepts five students id,name,age,department,courses, taken and its grade. a. calculate its GPA . display all student information in ascending order
Write java program?
Write java program?  1 11 121 1331 14641 15101051
write the java program?
write the java program?  1 11 121 1331 14641 15101051
write the java program?
write the java program?  1 11 121 1331 14641 15101051
Write java program?
Write java program?  1 11 121 1331 14641 15101051
Write a java application program........?
Write a java application program........?  Welcome every One : I have Q in Java? Q : Write a java application program that reads 20 integer numbers input by the user and prints how many numbers > 100 and how many numbers
write a program to print
write a program to print   write a program to print following 1 2 3 4 5 4 3 2 1 1 2 3 4 4 3 2 1 1 2 3 3 2 1 1 2 2 1 1 1
Write a program in java...
Write a program in java...  Hi, friends Please, can you help me? Q1: Write a program in java to simulate a calculator. Your program should take two... to enter an integer number. Write a program in java to find the factorial
how to write this program
how to write this program  Write a program in java which calculates... or numerical grade. You will be submitting TWO versions of this program. Write one...?¢s GPA. This program should take a studentââ?¬â?¢s First Name, Last Name a GPA
WRITE A PROGRAM IN JAVA
WRITE A PROGRAM IN JAVA  How do I write a program in Java, have the program display a message with your name in it and a number (Hello john Smith!1) The number must increment from zero to nine, or decrement fron nine to zero. You
write program - Java Beginners
write program  write a java program that will read the values of matrix A and B give its multiplication in matrix C  Hi friend, Code for Matrix Multiplication : class MatrixMultiplication{ public static void
write a program in java.
write a program in java.  arrange the natural n umber in 5x5 matrix as 21 22 23 24 25 20 7 8 9 10 19 6 1 2 11 18 5 4 3 12 17 16 15 14 13 i at centerd position and remaining arrange in anticlockwise direction.  
write following program
write following program  Q1 What is the difference between single...-threading. Explain with examples Q2 Write a java applet that takes your name through a parameter from the HTML file and displays it Q3 Write a short note
Write a program to print using for loop
Write a program to print using for loop  Write a program to print using for loop * * * *  
How to write calculator in J2ME program?
How to write calculator in J2ME program?  How to write calculator in J2ME program
how to write servlet program in eclipse?
how to write servlet program in eclipse?  how to write servlet program in eclipse
program problem - Java Beginners
program problem  Design and write a Java application that takes as input a single letter and displays the corresponding digit on the telephone. The letters and digits on a telephone are grouped this way: 2 = ABC 4 = GHI 6
problem in servlet program
problem in servlet program  Dear Sir, I have a problem to insert the Blob type data like as video file, audio file in the database using Servlet and html code
Java program problem
Java program problem  Hi, I took the chance to login here in your.... Please help me. I provided below the program assignment. Thanks in advance. Create program that: Will search through two directory structures and produce
write a program to create a user defined
write a program to create a user defined   plz send me code   Hi Friend, Please specify what do you want to create?ADS_TO_REPLACE_1 Thanks
how to write this program? - Java Beginners
how to write this program?  (game:scissor,rock ,paper) A program that plays the sciccor-rock-paper game.Revise the program to let the user continuously play until either the user or the computer wins more than 4 times
to write a program to display the employee details..
to write a program to display the employee details..  To dipslay... running this program the new window has to be opened asking us to enter all...   not this one actually i wnat a very small program that displays all
how to write the program - Java Beginners
how to write the program  WAP to create the report card user input Name, Class, Division, Roll no., Marks obtained in following subjects Lang Hindi History Geography Math Phy Chem. Bio Eve CSTA Also has a back up
i have problem in that program - JavaMail
i have problem in that program  1. Write a multi-threaded Java program to print all numbers below 100,000 that are both prime and fibonacci number (some examples are 2, 3, 5, 13, etc.). Design a thread that generates prime
servlet program problem - Java Beginners
servlet program problem   i used ur servlet example prg with xml file of helloworld program and i run dat program in tomcat, it shows only... file to run it  Hi Friend, Please clarify your problem. Thanks
write a program in C to print following triangles on the screen
write a program in C to print following triangles on the screen  write a program in C to print following triangles on the screen
write a program to demonstrate wrapper class and its methods......
write a program to demonstrate wrapper class and its methods......  write a program to demonstrate wrapper class and its methods
write a program to use for loop print **********========== in c language
write a program to use for loop print **********========== in c language  write a program to use for loop print **========== in c language
Write a program to get student details and store in a database
Write a program to get student details and store in a database  Write a program to get student details and store in a database
how to write a program to search a record using mvc2
how to write a program to search a record using mvc2   how can we write a code to search a record in table by using java bean as model, servlet as contoller and jsp as view
Write a program that takes the marks of user as input
Write a program that takes the marks of user as input  Write a program that takes the marks of user as input. If marks are greater then 60... not understand how to write a program like this and thank you
How to write a program count total prices
How to write a program count total prices  Hi. all I've got a assignment to write a program that count total prices of 8 costumers. First class...("-------------------------------------------"); } } } The problem is I'm being absolutely
write a java program to solve the infix expression
write a java program to solve the infix expression  ==>> solve the below expression using java program: 2+5*2-5+6/2 using stack operations or any other process in java
write a program for complete JDBC based application
write a program for complete JDBC based application   thank you sir giving answer for my question. my another question is: Write a complete JDBC... and the program should have the facilities of Adding, Modifying, Viewing and Deleting
Help me to write this simple java frame program
Help me to write this simple java frame program   I want to write a Java program for.... To create a frame with 4 text fields;name,street,city and pin;with suitable labels. Also add a button "OK". After enter values
Write a java program that prints out the following menu???
Write a java program that prints out the following menu???  Write a java program that prints out the following menu. Auser can interact with the menu using numbers (1,2,3,4,5,6). The menu will be printed again if the user enters

Ads