Eclipse Vs console output help

Well i have this program of producer consumer where bith classes share a common data class and both producer consumer have a thread implemented to have their respective functionality... here is the data class

publicclass Data{

private String content;

privateboolean avaliable=false;

synchronizedpublicvoid putContent(String part){

if(avaliable==true){

try{

wait(1000);

}catch(InterruptedException ie){

ie.printStackTrace();

}

}

content=part;

avaliable=true;

notifyAll();

}

synchronizedpublic String getContents(){

while(avaliable==false){

try{

wait(1000);

}catch(InterruptedException ie){

ie.printStackTrace();

}

}

avaliable=false;

return content;

}

}

Now here both the producer consumer threads access the synchronized methods, now when i run the whole program through command line in console iam getting requried result , but when iam running same thing in eclipse its not synchronizing and ouptut isnt coming correct.... i wonder what the reason could be, anyone who can help me through this? Thank you

[2383 byte] By [power-extremea] at [2007-11-27 9:18:11]
# 1

Eclipse - indeed any IDE - console views, are not actually consoles. They're just UI widgets (an SWT Composite in this case) which have the output re-directed to them. The output is quite unreliable, particularly with threads,as you've seen. Streams may not be flushed when you expect, for example, if at all. Your actual console view is correct

georgemca at 2007-7-12 22:09:12 > top of Java-index,Java Essentials,New To Java...
# 2

Well my console is givig output in all the cases but my IDE's (eclipse) case it working fine when i dont use the notifyAll() or notify() methods.. when i run it without these methods it gives correct ouptut but just as i put in these methods .. it looses the synchronization factor somehow.. whereas when i run through command line it all runs well... so what could be the possible reason?

Even if i run the program over and over again in console it gives wrong result once in a while.. so is it normal with threads? i have used synchronization and that class code is given above .. 2 threads have for loops calling the above given class 10times... so if i execute the program 4-5 times it gives a wroing output once... is that alrite or something wrong?

Message was edited by:

power-extreme

Message was edited by:

power-extreme

power-extremea at 2007-7-12 22:09:12 > top of Java-index,Java Essentials,New To Java...
# 3

Somebody please can you tell me .. is it possible that when i have made synchronized methods and having 2 threads call on it.. then can it be that once in a while it may give wrong output.. as in my case it does give if i try running many times the same program... is it because of the OS scheduling.. and is it normal or my code is not right?

power-extremea at 2007-7-12 22:09:12 > top of Java-index,Java Essentials,New To Java...
# 4
never rely on threads running in any particular order or sequence. You'll need to guard against the queue being in a state either producer or consumer can't handle explicitly by checking if it's for example empty and going back to sleep if it is.
jwentinga at 2007-7-12 22:09:12 > top of Java-index,Java Essentials,New To Java...