LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-29-2004, 02:46 PM   #1
mcshen
LQ Newbie
 
Registered: Dec 2003
Distribution: womandrake
Posts: 28

Rep: Reputation: 15
java timer


How do I implement a timer?

like

I wanna to tell my program to run for 100 seconds

after than, System.out.println("out of time");
 
Old 03-29-2004, 08:27 PM   #2
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
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".

Hope that helps,

Laura
 
Old 03-29-2004, 08:30 PM   #3
mcshen
LQ Newbie
 
Registered: Dec 2003
Distribution: womandrake
Posts: 28

Original Poster
Rep: Reputation: 15
ah, i see!
 
Old 03-29-2004, 09:39 PM   #4
moeminhtun
Member
 
Registered: Dec 2002
Location: Singapore
Distribution: Fedora Core 6
Posts: 647

Rep: Reputation: 30
You can use java.util.Timer or javax.swing.Timer.

javax.swing.Timer is easier.
 
Old 03-29-2004, 09:59 PM   #5
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
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.
 
Old 03-29-2004, 10:12 PM   #6
moeminhtun
Member
 
Registered: Dec 2002
Location: Singapore
Distribution: Fedora Core 6
Posts: 647

Rep: Reputation: 30
Quote:
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.
 
Old 03-30-2004, 01:17 AM   #7
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
Re: java timer

Quote:
Originally posted by mcshen
I wanna to tell my program to run for 100 seconds

after than, System.out.println("out of time");
Other than that, I think only mcshen can tell us what else needs to be done

-Laura
 
Old 03-30-2004, 01:18 AM   #8
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
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.
 
Old 03-30-2004, 08:11 AM   #9
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
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?
 
Old 03-30-2004, 10:20 AM   #10
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
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.
 
Old 03-30-2004, 02:47 PM   #11
mcshen
LQ Newbie
 
Registered: Dec 2003
Distribution: womandrake
Posts: 28

Original Poster
Rep: Reputation: 15
all i wanted it to do this:

i am making a game. the time limit for that game is, say 1000msec. When 1000msec is gone, program terminates (dispose() or System.exit(0))

Thanks for the help guys/gals..
 
Old 03-30-2004, 04:07 PM   #12
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
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
	}
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
First timer Cyberian Linux From Scratch 3 04-19-2005 12:27 PM
How do you stop this particular timer? JAVA randomx Programming 4 07-24-2004 05:36 AM
Java Timer alaios Programming 2 05-24-2004 01:39 PM
Java Timer alaios Programming 3 05-24-2004 11:52 AM
First timer, help!!!!! jcniest5 Linux - Newbie 7 02-24-2004 08:32 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration