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
Subscribe to Tutorials
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. Hi there,
this doesnt work in IE7 or firefo
View Tutorial By: Gerrard at 2009-10-17 15:19:24
2. i like this site.good explanation,but it is good f
View Tutorial By: mamata at 2010-08-16 05:22:53
3. I've been learning C++/C for the past year or so,
View Tutorial By: William Semple at 2013-01-05 17:53:30
4. it very useful to under stand
View Tutorial By: gurav gupta at 2009-09-24 00:20:15
5. Sir, this is very helpful for students and profess
View Tutorial By: Pankaj Choudhary at 2010-10-19 00:24:36
6. Hiiii Al, I am Ashraf Abu Aisheh, i am working to
View Tutorial By: Ashraf Abu Aisheh at 2008-11-09 14:56:39
7. Thanks ,It is very useful...
View Tutorial By: SABARISH at 2014-08-08 05:46:43
8. BIG Thanks for such wonderful tutorial :) :) :)
View Tutorial By: dhruva at 2013-07-10 08:56:27
9. Thx for the explanation, I understand what recursi
View Tutorial By: Tina at 2008-12-15 16:39:27
10. we are getting an error saying that " the sta
View Tutorial By: chaitra at 2014-03-20 10:53:02