If i have a thread something like this:
Thread t = new Thread(myThread);
t.start();
private static Runnable myThread = new Runnable() {
public void run() {
try{
String line=null;
while ((line = eStream.readLine()) != null){
//do something
}
}
catch (Exception e){}
}
};
Now what happens in while loop, at times the readLine()
is hanged because it is expecting input from external process. So in that case what i am trying to do is to setup a timer and if it expires, i interrupt this thread t
with t.interrupt();
Now when i try to catch InterruptedException
after the try block, i am getting compile time error. Can someone tell me in this scenario, how can i catch Interrupted Exception?
Thank you
interrupt won't free a blocking readLine() call. You have to close the stream to do that.
You are catching the InterruptedException already (and ignoring it) with this line
This won't work
As it will throw a NullPointerException when you reach the end of file. Remove the
.toString()