Share on Google+Share on Google+

aditya sharma
java multithreading
1 Answer(s)      6 years and 9 months ago
Posted in : Java Beginners
can i please get the program code for "producer consumer" problem using multithreading

Ads

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
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
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
Tutorials   
Java Spring Hibernate Struts Training What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? neo4j-server.properties file location SASLError using PLAIN: not-authorized how to convert war file into .exe file using java code

Ads

 
Advertisement null

Ads