File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes about notify() and wait() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Groovy Fundamentals video training course this week in the Groovy forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "about notify() and wait()" Watch "about notify() and wait()" New topic

about notify() and wait()

jeroen riezebeek

Joined: Nov 26, 2002
Posts: 9
i'm just getting some feeling with threads.
Is it possible for sombody to check if the code is correct?

Thanks in advance.
Joe Ess

Joined: Oct 29, 2001
Posts: 9101

Originally posted by jeroen riezebeek:
i'm just getting some feeling with threads.
Is it possible for sombody to check if the code is correct?

That depends on what you want it to do. Right now the two threads appear to run in lock step, one thread producing an item on a queue, the other thread being notified and consuming the item. The queue really isn't a queue since it only ever holds one item. After adding the producing thread encounters a wait(). If the consumer thread never invoked notifyAll(), the first thread would block forever, having produced one item. Have you checked out the
Java Tutorial chapter on Threads? There's a good producer-consumer example there, though it doesn't go as far as to use a queue. Maybe you could extend the example they give.

[How To Ask Questions On JavaRanch]
Yan Lee
Ranch Hand

Joined: Sep 15, 2003
Posts: 94
Hi jeroen:
Few comments regarding your code:
In class Queue:-
private LinkedList list = new Vector();
do not instantiate a Vector and assign it to a LinkedList Reference
it will give a compilation error
Instead use one of the following:-
1. Linkedlist ll=new LinkedList(); --OR--
2. List l=new LinkedList();

in class ThreadOne and ThreadTwo
Instantiate the queue object else you will get a compilation error
in class ThreadOverall:
Synchronization is used to allow only one thread to access a particular Queue object. In your current implementation,
both the ThreadOne instance and ThreadTwo instance access 2 different instances of the Queue object, so you are actually
not using synchronization.

To explain the same example in a simpler way, I have modified your programs as follows.
1. Like before ThreadOverall contains the main() class that instantiates the thread objects and starts them
2. ThreadOne is the class that writes data to the list and notifies any object waiting on the list that the lock for the list object is availible
3. ThreadTwo is the class that waits for ThreadOne to write the data into the list, only after ThreadOne is done writing the data, ThreadTwo is notified and it reads the contents of the list.
I hope that it helps. Below are the modified programs.
package waitNotify;
import java.util.LinkedList;
/*main class that instantiates the threads and starts them*/
public class ThreadOverall
public static void main(String args[])
LinkedList list=new LinkedList();

/*Both one and two are trying to access the same Queue object*/
ThreadOne one = new ThreadOne(list);
ThreadTwo two = new ThreadTwo(list);
package waitNotify;
import java.util.LinkedList;

* This class writes the data to the list, once it is done writing
public class ThreadOne extends Thread {
LinkedList list;
public ThreadOne(LinkedList ll) {
list = ll;
public void run() {
//once lock on queue object availible, get the lock on it
synchronized (list) {
// thread is eligible to run but if lock on queue not availible wait() on the queue object
System.out.println("writing data...");
for (int i = 0; i < 10; i++) {
list.add(new Integer(i).toString());


package waitNotify;
import java.util.LinkedList;
import java.util.ListIterator;
/**This class reads the data in the linked list, it waits for the writing thread,
* once it is notified, it starts reading and displays the contents of the list
* */
public class ThreadTwo extends Thread {
LinkedList list;
public ThreadTwo(LinkedList ll) {
list =ll;

public void run() {
synchronized (list) {
try {
System.out.println("Waiting to read...");
} catch (InterruptedException ie) {

System.out.println("reading data...");
ListIterator li=list.listIterator();


lian kun

Joined: Feb 25, 2004
Posts: 2
I agree. Here's the link:
subject: about notify() and wait()