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



Home Answers Viewqa Java-Beginners java multithreading
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


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


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

Advertisement
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... OS. Multithreading: Multithreading is a technique that allows
Advertisements
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... by a single OS. Multithreading : Multithreading is a technique
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
multithreading
multithreading  is multithreading is inbuilt in java? if yes then why we implement thread seperately
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
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
multitasking and multithreading
multitasking and multithreading  what is the difference between multitasking and multithreading
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
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
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
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 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
Advertisements
 

 

 

DMCA.com