Home Answers Viewqa Java-Beginners java multithreading

 
 


aditya sharma
java multithreading
1 Answer(s)      2 years and 10 months ago
Posted in : Java Beginners

can i please get the program code for "producer consumer" problem using multithreading
View Answers

July 13, 2010 at 11:08 AM


Hi Friend,

Try the following code:

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

Thanks









Related Pages:
multithreading
multithreading  is multithreading is inbuilt in java? if yes then why we implement thread seperately
Multithreading in Java
Multithreading in Java      ... a single thread. Lets us know about the concept of multithreading and learn... OS. Multithreading: Multithreading is a technique that allows
java multithreading - Java Beginners
java multithreading  can i please get the program code for "producer... the following code: class Multithreading { int val; boolean value = false... Runnable { Multithreading th; Producer(Multithreading th) { this.th = th; new
Describe synchronization in respect to multithreading.
Describe synchronization in respect to multithreading.  Hi, Describe synchronization in respect to multithreading. thanks,   Are you eager the search related to Java programming query. Let us check
Multithreading in Java
Multithreading in Java      ... thread. Lets us know about the concept of multithreading and learn.... Multithreading : Multithreading is a technique that allows a program
Multithreading ? - Java Interview Questions
Multithreading ?   Hi Friends, I am new to java , am not clear with Multithreading. Multithread will cause deadlock. We can...; Hi friend, Multithreading is a technique that allows a program
Synchronization with Multithreading - Java Beginners
method. Then how can u achieve multithreading.   Hi friend... information on Thread visit to : http://www.roseindia.net/java/thread/SynchronizedThreads.shtml http://www.roseindia.net/java/thread/ Thanks
MultiThreading
MultiThreading  In MultiThreading....I'm using Hashmap ....Is there any deadlock occurs?   HashMap has synchronization issues in multithreading. Simultaneous access to hash map must be handled by the programmer
Java Multithreading
Java Multithreading       Multithreading allows two parts of the same program to run concurrently. In Java, the Java Virtual Machine (JVM) allows an application to have multiple
Multithreading in Java
Multithreading in java is running multiple threads sharing same address space... by the operating system. A thread never exists on its own. Multithreading allows..., multitasking can be carried out in two ways: Multiprocessing Multithreading
Multithreading in Java
Multithreading in Java      ... a single thread. Lets us know about the concept of multithreading and learn... OS. Multithreading: Multithreading is a technique that allows
Java Multithreading Example
Java Multithreading Example In this section we will discuss how we can do a different task using the different threads. In Java Multithreading we can create multiple threads to run different tasks. This example will demonstrate you
multitasking and multithreading
multitasking and multithreading  what is the difference between multitasking and multithreading
wap for multithreading
wap for multithreading  wap for multithreading   Hi Friend, Try the following code: class Multithreading { int val; boolean value = false... { Multithreading th; Producer(Multithreading th) { this.th = th; new Thread
java - Java Interview Questions
java  java's multithreading system is built upon_____class and________interface
Thread priority in java
Thread priority in java A thread is a part or entity of a  process that  is scheduled for execution. As we know java is a multithreading... concurrently. In java each and every thread has priority , priority means which
Java multi-threading
Java multi-threading  How does multithreading take place on a computer with a single CPU
java program - Java Beginners
java program  1.write a program to show traffic signal using multithreading. 2.Except an integer from 1-12 and display corresponding month. if the integer is not between 1-12 then give the errer message and promit
Networking in java - Java Server Faces Questions
Networking in java  I have project named screen capture & controlling..How to use multithreading to connect different clients with server & how to handle
Java - Java Interview Questions
Java  Wat is use of multithreading concept in java  Hi Friend, Please visit the following links: http://www.roseindia.net/java/thread/Java-Multithreading.shtml http://www.roseindia.net/java/thread
java
java  what is java   Java is a programming language..., and business applications. Advantages of Java: * Java is simple, easy to design... programming languages. * Java is object-oriented, that is used to build modular
java - JSP-Servlet
is the difference between multitasking and multithreading
Java Program - Java Beginners
Java Program  Write a program that demonstrates the use of multithreading with the use of three counters with three threads defined for each. Three threads should represent the counters as follows : 1) One counter starts from
Java Program - Java Beginners
Java Program  Write a program that demonstrates the use of multithreading with the use of three counters with three threads defined for each. Three threads should represent the counters as follows : 1) One counter starts from
Browser tire - Java Interview Questions
Browser tire  If we use multithreading in my application then which tire is needed on clients browser so that application run successfully? Plz help me ass soon as possible
a java program
through the link may, this will be helpful for you http://www.roseindia.net/java/thread/java-multithreading-example.shtml Thanks...a java program  Write a java program that accepts positive numbers
core
core  where an multythread using   Please go through the following link: Java Multithreading
Java Thread - Java Beginners
and simple examples of "Multithreading". 1. http://www.roseindia.net/java/thread/index.shtml 2. http://www.roseindia.net/java/thread...Java Thread  hii i feel confusion in tread. i want to know about 1
Java MultiThread
Java has multithreading feature. Multithreading feature is given by Thread class and Runnable interface Java Multithreading allows to run multiple tasks(threads) at a time. Java Multi Thread Example public class multi
java - Java Interview Questions
and Servlet Servlets are based upon multithreading .It can support multiple... support multithreading. For read in more details : http...;Servlets are effectively a Java version of CGI scripts, which are written in Perl, C
java - Java Beginners
the previous and the latest version. what is meant by multithreading... Multithreading : Multithreading is a technique that allows a program.... For more information on Thread visit to : http://www.roseindia.net/java/thread
java multithread - Java Beginners
java multithread  Hi, Thanks for your multithreading java code. It helped me a lot. But i have one issue. My code is look like this. public class classname extends Thread { public classname() { super
java - Java Interview Questions
can get singleton java class.That mean that java class will allow one object to access that class. With this we can avoid multithreading.This Singleton java.... to avoid multiple transaction at a time(to avoid multithreading). Thanks you
multi threading - Java Interview Questions
information over Multithreading concepts visit http://www.roseindia.net/java/thread... of multithreading there are two ways to implement. 1. Extending the Thread Class
Java Thread class
Java Thread Class is a piece of the program execution Java has multithreading facility. It allows multiple works(threads) to run at a time It is created by extending the Thread class or implementing Runnable interface Java
producer and concumer Interface program
java.util.*; class Multithreading { int val; boolean value = false; synchronized int...: " + val); notify(); } } class Producer implements Runnable { Multithreading th; Producer(Multithreading th) { this.th = th; new Thread(this, "Producer").start
Java Glossary Term - M - Java Important Terms - Java Programming Glossary
M       Java Map  In Java, a Map is a kind of generalized array. It provides a more... duplicate keys.   Java Mail JavaMail includes APIs
programs - Java Beginners
to implement an Animal Abstract class. 7. Write a Java program an multithreading by using Runnable interface. 8. Write a Java program on multithreading by sung.... (by using methods of minimum and maximum of numbers) 3. Write a Java program
programs - Java Beginners
to implement an Animal Abstract class. 7. Write a Java program an multithreading by using Runnable interface. 8. Write a Java program on multithreading by sung.... (by using methods of minimum and maximum of numbers) 3. Write a Java program
Java Features
. This is how Multithreading works in Java which you will soon come to know in details...Peculiarities of Java Platform Independent: The concept of Write-once-run... of java language that makes java as the most powerful language. Not even
java features
. Java is also a Multithreaded programming language. Multithreading means a single... threads and they run on the system CPUs. This is how Multithreading works in Java... Peculiarities of Java ..............   
Java Features
; Multithreading programming is a very interesting concept in Java... on the system CPUs. This is how Multithreading works in Java which you... Peculiarities of Java ...      
Core-Java - Development process
using multithreading here thanks, Jitendra Dixit. dixit.jitendra@gmail.com.../java
Java Features
. This is how Multithreading works in Java which you will soon come to know in details... Java Features      ... as the Platform independent) is one of the important key feature of java language
Java Features
. Multithreaded : Java is a multithreaded programming language. Multithreading means...Java Features In this section we will discuss about the  feature of java, they are as follows. Simple : Java is a very simple language, syntax
Threading in Java
Threading in Java      ... is very important in Java Programing language. A thread is a sequential path... to Multithreading Multithreading is a technique that allows a program or a process
Thread and Process - Java Beginners
. In most multithreading operating systems, a process gets its own memory address...://www.roseindia.net/java
Features of Java
technique to improve performance. Multithreaded: Java supports Multithreading, which... Java is a class based Object Oriented Programming (OOPs) language, which...-browsers today come with Java plug-in supports. Following are the basic
Java Runnable Interface
Java Runnable Thread is a piece of the program execution. Java Runnable is an interface. Thread class implements it. Java has multithreading facility. Thread is also created by implementing the Runnable interface. Java

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.