aspose file tools*
The moose likes Threads and Synchronization and the fly likes wait() causing problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "wait() causing problems" Watch "wait() causing problems" New topic
Author

wait() causing problems

Amit A. Patil
Ranch Hand

Joined: May 04, 2006
Posts: 38
Hi ,
When i run the below code i keep getting

java.util.NoSuchElementException
at java.util.LinkedList.remove(Unknown Sour
at java.util.LinkedList.removeFirst(Unknown
at Test$1.run(Test.java:48)
at java.lang.Thread.run(Unknown Source)

When logically what i understand it should?Any ideas why?

import java.io.IOException;
import java.io.File;
import java.util.LinkedList;
import java.util.List;


public class Test{


static LinkedList msgQueue=new LinkedList();
public static void main(String[] args) throws Exception{
Integer dr = new Integer("2");

forkThread();
forkThread();
enQueue(dr);
enQueue(dr);
enQueue(dr);

Thread.sleep(1000);
enQueue(dr);
enQueue(dr);
enQueue(dr);
System.out.println("The Thread is MAIN WALA"+Thread.currentThread().getName());
System.out.println("The End");


}

public static void forkThread(){
Thread thrd=new Thread(
new Runnable(){
public void run(){
System.out.println("The Thread is----------------"+Thread.currentThread().getName());
while(true){
System.out.println("The Thread is****************"+Thread.currentThread

().getName()+msgQueue.isEmpty());
synchronized (msgQueue) {
try {
if(msgQueue.isEmpty()){
System.out.println("Waiting!!");
msgQueue.wait();
System.out.println("The List"+msgQueue.isEmpty());
}
System.out.println("The Thread

is++++++++++++++++++"+Thread.currentThread().getName()+msgQueue.isEmpty());
Integer popDr=(Integer)msgQueue.removeFirst();
System.out.println("Dequeuing "+popDr);

} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}



}

}
}
}
);
thrd.start();
}

public static boolean enQueue(Integer dr){
System.out.println("Enqueing the messsage((((((((((((((");
synchronized (msgQueue) {
boolean ntfyall=msgQueue.isEmpty();
msgQueue.add(dr);
System.out.println("The EMPTINESSS"+ntfyall);
if(ntfyall)
msgQueue.notifyAll();
return true;
}

}

}
Amit A. Patil
Ranch Hand

Joined: May 04, 2006
Posts: 38
by the way changing
if(msgQueue.isEmpty()){
to
while(msgQueue.isEmpty())

works cleanly
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18999
    
  40

The main cause of your problem is the usage of the notifyAll() method. If there are more than one thread waiting, then you wake them all up. So if you put one object in the queue, the first one the wake up will take it while the others will get the error.

However, even if you change it to notify(), this can still happen -- although less likely. There is a chance that the one thread can get the notification, but before it reacquires the lock and wakes up. Another thread checks for empty, finds it not, and processes the data.

Changing it from an if to a while is the right solution. You probably don't need the notifyAll -- a notify should work.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Amit A. Patil
Ranch Hand

Joined: May 04, 2006
Posts: 38
You are spot on .Dont know how i missed that one.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wait() causing problems