java multithreading

java multithreading

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 Tutorials/Questions & Answers:
What is Multithreading in Java?
What is Multithreading in Java?  What is Multithreading in Java? Can any one explain me the concept of What is Multithreading in Java? Thanks  ... simultaneously. Read more at Multithreading in Java. Thanks
Multithreading in Java
Multithreading in Java      ... a single thread. Lets us know about the concept of multithreading and learn... processes by a single OS. Multithreading: Multithreading is a technique
Advertisements
multithreading
multithreading  is multithreading is inbuilt in java? if yes then why we implement thread seperately
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
Multithreading in Java
Multithreading in Java     ... thread. Lets us know about the concept of multithreading and learn... heavyweight processes by a single OS. Multithreading : Multithreading
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
What is Multithreading in Java?
What is Multithreading in Java and how to create Multi threaded...; In this tutorial we are going to discuss the multithreading concept of Java. You will learn What is Multithreading in Java and how to create Multi
What is Multithreading in Java?
What is Multithreading in Java and how to create Multi threaded...; In this tutorial we are going to discuss the multithreading concept of Java. You will learn What is Multithreading in Java and how to create Multi
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... Multithreading Multithreading allows a process to run its tasks in parallel mode
Multithreading in Java
Multithreading in Java      ... a single thread. Lets us know about the concept of multithreading and learn... processes by a single OS. Multithreading: Multithreading is a technique
I really need a tutor for Java program that has to do with multithreading and gui!
I really need a tutor for Java program that has to do with multithreading and gui!  I am looking for a Tutor to help me with a Java program, specially GUI and multithreading. If you can tutor, please email me
Multithreading Java Tutorial for Beginners
Multithreading in Java means two or more parts of program run simultaneously. Every single part is called thread. Running them simultaneously saves time. Different threads run in simultaneous mode. Multithreading Java tutorials
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
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
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:ADS_TO_REPLACE_1 class Multithreading { int val... Runnable { Multithreading th; Producer(Multithreading th) { this.th = th; new
Multithreading Example In Java
Multithreading Example In Java In this section we will learn about multithreading in Java. Multithreading in Java is used to execute multiple tasks at the same time or parallel. Support of Multithreading in Java allows to process
How do servlets work? Instantiation, session variables and multithreading
How do servlets work? Instantiation, session variables and multithreading  How do servlets work? Instantiation, session variables and multithreading
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
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 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
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
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
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
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 - 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
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 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  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 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
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
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
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
Core-Java - Development process
using multithreading here thanks, Jitendra Dixit. [email protected].../java
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
New to Java programming
- In Java multithreading can be used to develop programs that runs in many... In Java and Java for Multithreading applications with Example for more...New to Java Programming? How to learn and master Java programming language
core
core  where an multythread using   Please go through the following link: Java Multithreading
What are the basics of Java
What are the basics of Java  Hi, I want to know the basics of Java... of Java? Thanks   What are the basics of Java? Java is a programming..., collections, and structures). Java is an open-source language. In order to make
java features
Multithreading programming is a very interesting concept in Java... and they run on the system CPUs. This is how Multithreading works in Java which you... Peculiarities of Java ..............   

Ads