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

[2177 byte] By [ashoknanashoka] at [2007-11-15]
# 1
Could it be the 8-second sleep? What's that for?
ejpa at 2007-7-12 > top of java,Core,Core APIs...
# 2
Maybe using java.lang.Thread.join() is help to you.
EUEEa at 2007-7-12 > top of java,Core,Core APIs...
# 3
Even if i remove the sleep i see the same behaviour.
ashoknanashoka at 2007-7-12 > top of java,Core,Core APIs...
# 4
show us your LoadCompsFTP class then, the error has got to be somewhere in there...
Dalzhima at 2007-7-12 > top of java,Core,Core APIs...
# 5

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

ashoknanashoka at 2007-7-12 > top of java,Core,Core APIs...
# 6

I have no idea except maybe that you're not seeing what you think you're seeing, but I would get rid of that 'invalid' flag and the catch block in the constructor and have the constructor throw whatever exceptions may arise. The way you have it you can construct an invalid object and inevitably you aren't even checking 'isValid' anywhere. Having the constructor throw an exception means the object can never even exist in this 'invalid' state.

ejpa at 2007-7-12 > top of java,Core,Core APIs...
# 7

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

ashoknanashoka at 2007-7-12 > top of java,Core,Core APIs...
# 8

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.

ashoknanashoka at 2007-7-12 > top of java,Core,Core APIs...
# 9

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.

ashoknanashoka at 2007-7-12 > top of java,Core,Core APIs...