S
Sadalsud
I'm writing a console application where the user has to enter some text at a
prompt within a set time period. If the user has not entered anything within
that time period, I need to interrupt that operation and move on to someting
else. I've tried doing it with the sample code below. I figured that closing
the input stream would cause an IOException to be raised for the current
read(), but that does not seem to be happening. If anyone has any suggestions,
it would be greatly appreciated. I'm using Java 1.4.2 on a Debian Linux system.
/*start sample code*/
/*I would like the first readLine() to be interrupted after 5 seconds, but it
is never interrupted. As expected though, I do get an exception on the 2nd
readLine(), but that doesn't do me much good*/
import java.util.*;
import java.io.*;
public class CancelingInput {
Timer t;
InputStreamReader inputStream;
BufferedReader reader;
String response;
public static void main(String args[]) throws Exception {
CancelingInput c = new CancelingInput();
}
public CancelingInput() throws Exception {
inputStream = new InputStreamReader(System.in);
reader = new BufferedReader(inputStream);
t = new Timer(true);
t.schedule(new ClosingTask(), 5*1000);
System.out.print("Wait 6 seconds to enter (I would like for this to be
interrupted after 5 seconds) text: ");
response = reader.readLine();
System.out.println("Input: " + response);
System.out.print("Now enter one last line of text: ");
response = reader.readLine();
System.out.println("Input: " + response);
}
class ClosingTask extends TimerTask {
public ClosingTask() {}
public void run() {
try {
inputStream.close();
reader.close();
}
catch(IOException e) {
System.out.println("Error closing streams");
}
}
}
}
prompt within a set time period. If the user has not entered anything within
that time period, I need to interrupt that operation and move on to someting
else. I've tried doing it with the sample code below. I figured that closing
the input stream would cause an IOException to be raised for the current
read(), but that does not seem to be happening. If anyone has any suggestions,
it would be greatly appreciated. I'm using Java 1.4.2 on a Debian Linux system.
/*start sample code*/
/*I would like the first readLine() to be interrupted after 5 seconds, but it
is never interrupted. As expected though, I do get an exception on the 2nd
readLine(), but that doesn't do me much good*/
import java.util.*;
import java.io.*;
public class CancelingInput {
Timer t;
InputStreamReader inputStream;
BufferedReader reader;
String response;
public static void main(String args[]) throws Exception {
CancelingInput c = new CancelingInput();
}
public CancelingInput() throws Exception {
inputStream = new InputStreamReader(System.in);
reader = new BufferedReader(inputStream);
t = new Timer(true);
t.schedule(new ClosingTask(), 5*1000);
System.out.print("Wait 6 seconds to enter (I would like for this to be
interrupted after 5 seconds) text: ");
response = reader.readLine();
System.out.println("Input: " + response);
System.out.print("Now enter one last line of text: ");
response = reader.readLine();
System.out.println("Input: " + response);
}
class ClosingTask extends TimerTask {
public ClosingTask() {}
public void run() {
try {
inputStream.close();
reader.close();
}
catch(IOException e) {
System.out.println("Error closing streams");
}
}
}
}