Why System.in is blocking other IO call
I was trying this simple program to run some test...
package com.jpmc.ibtech.hycee.base;
import java.io.IOException;
publicclass TestThread{
/**
* @param args
* @throws IOException
*/
publicstaticvoid main(String[] args)throws IOException{
(new Thread(){
publicvoid run(){
try{
sleep(8000);
LoadCompsFTP ftpc =new LoadCompsFTP();
ftpc.listNames();
}catch (Exception e){
System.out.println("exception!!");
}
}
}).start();
System.out.println("test1");
try{
int b = System.in.read();
System.out.println("read data from main thread=" + b);
}catch (Exception e){
System.out.println("exception!!");
}
}
LoadCompsFTP() class uses apache commons FTP lib to connect to FTP server.What i observed is when main thread waits on the Systtem.in the connect method in the FTP lib is waiting..It connect's only after i enter anything on my input.basically after system.in ends.
I am not sure why.
I am running JDK 1.4.2 on windows XP
public LoadCompsFTP() {
System.out.println("Creating FTP");
ftpClient = new FTPClient();
isValid = true;
try {
System.out.println("Connecting FTP");
ftpClient.connect("ftpgate.se.com");
ftpClient.login("moran" + "@" + "ftp.omp.com","589");
System.out.println("login FTP");
} catch (Exception e) {
isValid = false;
}
}
public String[] listNames() throws IOException {
System.out.println("Asking filename");
String[] fileNames = ftpClient.listNames();
System.out.println("Got filename");
if (fileNames == null) {
return new String[0];
} else {
return fileNames;
}
}
Message was edited by:
ashoknanashok
What i am excepting is....
I should be able to get all the filenames from ftp server..while main thread is waiting in the system.in.read....I am assuming this should happen bcos FTP pull is happening in the seperate thread.But what i observed is FTP pull is happening only after system.in.read compeltes..meaning after if i enter some input in the command line.
(Note:I am running this class from Eclipse as Java Application.)
What a weird thing...It's happening only if i run it thru Eclipse(Run as Java App)
If i run the same class from windows command prompt ...everything is working as expected...
Even i observed the same thing using JPROFLIER..when i set the console to java then FTP is waiting untill System.in.read to complete..
When i set the console (Native windows only) then everything started working.
Finally i was able to track down the problem...Read the entire postILink below) to see why this is happening in Eclipse enviornment
Look at this...
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4809647
One thing i am not able to figure out in Jprofiler is..class loader wait for System.in.read to complete.