Comment on Tutorial - wait(), notify() and notifyAll() in Java - A tutorial By Jagan
Comment Added by : Hdm-Student
Comment Added at : 2010-06-16 08:10:51
Comment on Tutorial : wait(), notify() and notifyAll() in Java - A tutorial By Jagan
Hello,
your solution is kinda overkill.
When used correctly, notify and wait don't need workarounds such as valueSet.
Here's the improved version:
----------------------
package producer;
// A correct implementation of a producer and consumer.
class Q {
int n;
synchronized int get() {
try {
notify();
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
System.out.println("Got: " + n);
return n;
}
synchronized void put(int n) {
try {
notify();
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
this.n = n;
System.out.println("Put: " + n);
}
}
public class Producer implements Runnable {
Q q;
Producer(Q q) {
this.q = q;
new Thread(this, "Producer").start();
}
public void run() {
int i = 0;
while (true) {
q.put(i++);
}
}
}
class Consumer implements Runnable {
Q q;
Consumer(Q q) {
this.q = q;
new Thread(this, "Consumer").start();
}
public void run() {
while (true) {
q.get();
}
}
}
class PCFixed {
public static void main(String args[]) {
Q q = new Q();
new Producer(q);
new Consumer(q);
System.out.println("Press Control-C to stop.");
}
}
-------------------------
View Tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Archived Comments
1. Thank You,
Code is working well.
Is
View Tutorial By: dinesh at 2012-06-23 04:33:16
2. Abstract class example is good, What is the differ
View Tutorial By: megakumar at 2011-04-10 04:15:10
3. @Vipin : vipin u r wrong,,,in reality java does n
View Tutorial By: Arpit Tyagi at 2010-07-23 04:11:20
4. Ku is quite correct. When you create any object, i
View Tutorial By: Avnish at 2010-10-28 04:00:20
5. I want to use concept of overriding and i want to
View Tutorial By: Aniket at 2013-06-20 10:23:10
6. I have tried that but still getting the same error
View Tutorial By: pra at 2008-04-14 21:55:44
7. really nice
View Tutorial By: umer at 2011-06-25 06:35:19
8. thanku very much. it is a good examples for abstr
View Tutorial By: rajesh at 2010-12-17 10:57:42
9. I will try
View Tutorial By: bridges at 2009-09-06 22:09:46
10. Hi,
I've tried this but got a problem. Firs
View Tutorial By: Hendrik Jabs at 2009-12-10 02:46:13