Hi
I using core
java and my application requires functionality that an input is requested at console and some timeout is set.i f no input is received before that interval program should exit
Can anyone pls help me out by example as hot to do it
Thnkx in advance
Gaurav
Here is what I have tried so far
import java.util.Random;
import java.io.*;
public class
Test {
public static void main(
String arg[]) throws IOException,InterruptedException
{
Random r = new Random();
Timer t = new Timer();
t.start();
int randInt = Math.abs(r.nextInt()) % 2;
System.out.println("Random No generation is "+randInt);
long timeInit = System.currentTimeMillis();
// System.out.println("Time init :"+timeInit);
long timeEnd=0;
System.out.println("enter the no:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = " ";
str=br.readLine();
if(t.getTime() >40)
{
System.out.println("Sorry time out exceeded");
System.exit(0);
}
}
}
where
Timerclass is
public class Timer extends
Thread {
private int theTime =0;
public Timer() {
super();
theTime =0;
} // End Timer constructor.
public void run() {
while (true) {
try {
this.sleep( 100);
} catch (InterruptedException exception) {
// Do nothing.
} // End try/ catch.
theTime++;
} // end while.
} // End run.
public int getTime() {
return theTime;
} // End getTime;
}
Problem is that my program hangs on till I put something in console to satisfy the br.readLine()
and only after that I can check whether time out occured or not
What I intended to do was in case time out occured I should have got msg of timeout withouts entering anything to satisfy the buffered reader
Can any of the wizards pls help me out
:roll:
[ April 10, 2003: Message edited by: Gaurav Chikara ]