Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to implement timeout using threads

 
Gaurav Chikara
Ranch Hand
Posts: 412
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless you're dedicated to implementing this yourself, you might check out java.util.Timer and java.util.TimerTask.
 
Joe Cheng
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you want the Timer thread to be the one that call System.exit(). In other words, the Timer thread sleeps for n milliseconds, wakes up and checks if any input has been entered (via the other thread), and if not then prints the timeout message and calls System.exit().
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start the reader in a thread that you dont mind being closed.
Use a 2nd thread to close the reader when/if the time runs out.
 
wei wu
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have compiled and runned this code, and it worked very well.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic