This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes How to implement timeout using threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How to implement timeout using threads" Watch "How to implement timeout using threads" New topic

How to implement timeout using threads

Gaurav Chikara
Ranch Hand

Joined: Jun 09, 2000
Posts: 410
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
Here is what I have tried so far
import java.util.Random;
public class Test {
public static void main(String arg[]) throws IOException,InterruptedException
Random r = new Random();
Timer t = new Timer();
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(;
String str = " ";

if(t.getTime() >40)
System.out.println("Sorry time out exceeded");
Timerclass is
public class Timer extends Thread {
private int theTime =0;
public Timer() {
theTime =0;
} // End Timer constructor.

public void run() {
while (true) {
try {
this.sleep( 100);
} catch (InterruptedException exception) {
// Do nothing.
} // End try/ catch.
} // 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
[ April 10, 2003: Message edited by: Gaurav Chikara ]

SCJP,SCWCD,SCBCD<br />If Opportunity doesn't knock then build the door
Greg Charles

Joined: Oct 01, 2001
Posts: 2963

Unless you're dedicated to implementing this yourself, you might check out java.util.Timer and java.util.TimerTask.
Joe Cheng

Joined: Feb 23, 2003
Posts: 11
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

Joined: Oct 05, 2001
Posts: 1170

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

Joined: Apr 27, 2003
Posts: 23

I have compiled and runned this code, and it worked very well.
It is sorta covered in the JavaRanch Style Guide.
subject: How to implement timeout using threads
It's not a secret anymore!