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;
}
}
}