Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Interview-Questions producer and concumer Interface program
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


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


I need producer and concumer Interface program


Advertisement
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



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   what is the use of marking interface
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.  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
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  how interface support multiple inheritance in java
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  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  What is marker interface ?? what is its use in java programming?? is this us in programming ??Explain is implementation with code
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
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
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...){ buf.add(item); items++; } } class Producer implements Runnable
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
collection interface
collection interface  methods of collection interface and their description with example program?   Please visit the following links: http://www.roseindia.net/java/jdk6/Collection-Interfaces.shtml http
collection interface
collection interface  methods of collection interface and their description with example program?   Please visit the following links: http://www.roseindia.net/java/jdk6/Collection-Interfaces.shtml http
i have created interface program for simple arithmetic operation but its show some error can i get reason and mistakes
i have created interface program for simple arithmetic operation but its show some error can i get reason and mistakes  import java.io.*; interface... is your code, we have modified it. import java.io.*; interface arith { int a=5
i have created interface program for simple arithmetic operation but its show some error can i get reason and mistakes
i have created interface program for simple arithmetic operation but its show some error can i get reason and mistakes  import java.io.*; interface... is your code, we have modified it. import java.io.*; interface arith { int a=5
What is an interface?
What is an interface?  What is an interface
Can an Interface extend another Interface?
Can an Interface extend another Interface?   Hi, Can an Interface extend another Interface? thanks
Can an Interface implement another Interface?
Can an Interface implement another Interface?   Hi, Can an Interface implement another Interface? Thanks
ServletRequest interface
ServletRequest interface   What is new in ServletRequest interface
Advertisements
 

 

 

DMCA.com