• Post Reply Bookmark Topic Watch Topic
  • New Topic

Time out in case nothing is read

 
Gaurav Chikara
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi in case I want to implement timeout functionality in java
suppose an inout is needed at console and it is not entered till certain time the program should exit
Can anyone pls help me out as I have to implement it in my project
Thankx in advance
:roll:
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the Timer and TimerTask classes. You can use to some sort of flag variable that when input is received, the flag is set. Your task basically checks if the flag is set, iterating over a specified period of time until the timer runs out. If the task determines that input was received, cancel out of the timer.
 
Gaurav Chikara
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it requires me to make my current class extend TimerTask class because TimerTask is an abstract class
Thnkx in advance
 
Gaurav Chikara
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone pls give me some link where I can find the working example of it pleease
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Either that class can extend TimerTask, or you can create a separate class that represents the task. However, doing that usually invovles needing some reference to the class you're going to be checking for received input. So unless your input reading class already extends something, I would go with the first option.
A simple example:

That's VERY basic, but hopefully can give you some idea of the usage of Timers. Tailor it to your own needs.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!