aspose file tools*
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
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How to implement timeout using threads" Watch "How to implement timeout using threads" New topic
Author

How to implement timeout using threads

Gaurav Chikara
Ranch Hand

Joined: Jun 09, 2000
Posts: 410
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 ]

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

Joined: Oct 01, 2001
Posts: 2861
    
  11

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

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
Greenhorn

Joined: Apr 27, 2003
Posts: 23

I have compiled and runned this code, and it worked very well.
 
Don't get me started about those stupid light bulbs.
 
subject: How to implement timeout using threads