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