• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

wait() causing problems

 
Amit A. Patil
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by the way changing
if(msgQueue.isEmpty()){
to
while(msgQueue.isEmpty())

works cleanly
 
Henry Wong
author
Marshal
Pie
Posts: 21493
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Amit A. Patil
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are spot on .Dont know how i missed that one.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic