Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to implement timeout using threads

 
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unless you're dedicated to implementing this yourself, you might check out java.util.Timer and java.util.TimerTask.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have compiled and runned this code, and it worked very well.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic