ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Well, one way would be to make your program threaded. Start a thread, have it start the main body of your program on another thread, have your thread wait for 100 seconds (or 100000 ms), and when it comes out of the wait, make it stop the program's thread and print "out of time".
I thought of that, and looked it up. However, the Timer class doesn't do what he seems to want. A Timer() will only accept a delay time 'til it starts - there's no way to control how long it runs (as far as I saw, I've not used it before /disclaimer)
-Laura
Last edited by rose_bud4201; 03-29-2004 at 10:00 PM.
Originally posted by rose_bud4201 I thought of that, and looked it up. However, the Timer class doesn't do what he seems to want. A Timer() will only accept a delay time 'til it starts - there's no way to control how long it runs (as far as I saw, I've not used it before /disclaimer)
-Laura
If that's the case, yeah, timer can't do the job, but he didn't stated what he want clearly. I think the timer could be the answer, not sure what he want.
You should be able to use a timer. Set the timer up to run every 100 secs, start the timer. do what every you want after 100 secs the messages gets output and you exit the program or do what ever it is you want to do. You can also cancel a timer to stop it running for ever.
Alright, cool.
In that case, this is purely for my information: how to you "time the timer" to tell it when to stop? The way it looked to me was you could give the timer a delay, but no time limit. So in this instance, would he sort of be using it in reverse? i.e. when the timer starts, end the program?
Which sort of leads to my next question: how do you detect when it starts?
Yah, in reverse - using it just like an Alarm Clock I guess. You don't as such have to detect when it starts, it'll start - if it's Simply exit the program after 100 seconds set the timer to run every 100 seconds, in the timer's body of code put whatever you want in it.
Here is a very simple example of using a Timer that uses a common variable to flag time is up, just for show, of course, and it's only a suggestion:
Code:
import java.util.*;
public class TimeMan{
static boolean stillTimeLeft=true;
static int count=0;
public static void main(String args[])
{
Timer tTimer=new Timer();
TimerTask tTask=new TimerTask(){
public void run(){
stillTimeLeft=false;
// System.exit(0); //or just exit the program
}
};
tTimer.schedule(tTask,10000); //start the timer running
do //do something for heavens sake :)
{
try{
Thread.sleep(1000);
System.out.println(""+ (++count));
}catch(Exception e){ };
}while(stillTimeLeft);
tTimer.cancel(); //stops the scheduled task or we'll be here for ever or the next
//power cut at least
}
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.