producer and concumer Interface program

Ads

 
 

Share on Google+Share on Google+

Bsharat
producer and concumer Interface program
1 Answer(s)      7 years ago
Posted in : Java Interview Questions

I need producer and concumer Interface program

Ads
View Answers

October 22, 2010 at 2:30 PM


Hi Friend,

Try the following code:

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);
}
}

For more information, visit the following link:

http://www.roseindia.net/java/thread/InterthreadCommunication.shtml

Thanks


Ads









Related Tutorials/Questions & Answers:
producer and concumer Interface program
producer and concumer Interface program  I need producer and concumer Interface program   Hi Friend, Try the following code: import...: " + val); notify(); } } class Producer implements Runnable { Multithreading th
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
Advertisements
program and Interface builder
program and Interface builder  Hi, I'm just getting started in iPhone development, If someone could please suggest a good criterion for when to use a nib to lay out UI elemennts and when to layout the UI elements
Interface
Interface  Can Any one Give the exact program explanation for Interface in java? and want to know why they used interface instead of multiple inheritance? Thanks in advance   An interface is one which has abstract
interface
interface   what is the use of marking interface
interface.
interface.  Write short note on interface.   Please visit the following link: Java Interface
interface
interface  will the interface implements a interface   Hi Friend, No. Interface can extends another interface but cannot implements it, because interface will not contain the implementation. Thanks
Write an event driven program to perform the arithmetic operations as shown in the interface
Write an event driven program to perform the arithmetic operations as shown... the java programming code for that question. my question is here: Q. Write an event driven program to perform the arithmetic operations as shown
INTERFACE
INTERFACE  how interface support multiple inheritance in java
interface
interface   Hi I have interface in that interface 3 methods are there , after some days client said that,i want to add one more method in that interface ,so how can add 4 method so that the implemented class did not affect
interface
interface  can we extend interface?   Hi Friend, Yes an interface can be extended by other interface like using extends keyword interface A{ } interface B extends A{ } For Example: interface IntefaceA { void
interface
interface  what the use of interface?   An interface is one... variables.Any class can implement(inherit)the interface and make use... is achieved by using the interface (by implementing more than one interface at a time
interface
interface  What is marker interface ?? what is its use in java programming?? is this us in programming ??Explain is implementation with code
interface
interface  develop a library interface which has drawbook(),returnbook()(with fine),checkstatus() and reservebook() methods.all the methods tagged with public
interface
interface  develop a library interface which has drawbook(),returnbook()(with fine),checkstatus() and reservebook() methods.all the methods tagged with public
interface
interface  why do we need interface in java..if it`s usefull for to obtain multiple inheritance in the sense how it gonna be achieved...and i can...)...the y we need interface...THis is question often i heard from my developer
Interface
Interface  I need to implement the interface without overriding its method and also don't use abstract class for this. How to do
interface
interface  What is the exact use of interface in real time scenario? some people says that interface provides multiple inheritance. Is it true...; Through interface, we can achieve the multiple inheritance. Java does
Interface
Interface  1.Create an interface names ShapeInterface that has two... class a.Rectangle that uses the interface b.Circle that uses the interface and abstract class   interface ShapeInterface{ int l=0,b=0; public
Interface
Interface  1.Create an interface names ShapeInterface that has two... class a.Rectangle that uses the interface b.Circle that uses the interface and abstract class   interface ShapeInterface{ int l=0,b=0; public
Interface
Interface  Declare an Interface called property containting a method compute price to compute &return the price.The inerface is to be implemented by follwaing two class (1)Bungalow&(2)Flat both the lasses have following
Version of be.looorent>jflu-producer-hibernate dependency
List of Version of be.looorent>jflu-producer-hibernate dependency
Version of be.looorent>jflu-producer-rabbitmq dependency
List of Version of be.looorent>jflu-producer-rabbitmq dependency
Version of be.looorent>jflu-producer-springmvc dependency
List of Version of be.looorent>jflu-producer-springmvc dependency
Version of com.amazonaws>amazon-kinesis-producer dependency
List of Version of com.amazonaws>amazon-kinesis-producer dependency
EJB remote interface
EJB remote interface       The program given below describes  the way of creating a remote interface...;} } Output of the Program Accessing Remote Interface using
Maven Dependency jflu-producer-hibernate >> 0.1
You should include the dependency code given in this page to add Maven Dependency of be.looorent >> jflu-producer-hibernate version0.1 in your project
Maven Dependency jflu-producer-rabbitmq >> 0.1
You should include the dependency code given in this page to add Maven Dependency of be.looorent >> jflu-producer-rabbitmq version0.1 in your project
Maven Dependency jflu-producer-springmvc >> 0.1
You should include the dependency code given in this page to add Maven Dependency of be.looorent >> jflu-producer-springmvc version0.1 in your project
Maven Dependency amazon-kinesis-producer >> 0.10.0
You should include the dependency code given in this page to add Maven Dependency of com.amazonaws >> amazon-kinesis-producer version0.10.0 in your project
Maven Dependency amazon-kinesis-producer >> 0.10.1
You should include the dependency code given in this page to add Maven Dependency of com.amazonaws >> amazon-kinesis-producer version0.10.1 in your project

Ads

 
Advertisement null

Ads