tags:

views:

474

answers:

4

Hiya. I have a specific function that i want to be executed after 5 seconds. how can i do that in java?

I found javax.swing.timer, but i can't really understand how to use it. it looks like i'm looking for something way simpler then this class provides.

please add a simple usage example.

thanks!!!!

+3  A: 
new java.util.Timer().schedule( 
        new java.util.TimerTask() {
            @Override
            public void run() {
                // your code here
            }
        }, 
        5000 
);

EDIT:

javadoc says:

After the last live reference to a Timer object goes away and all outstanding tasks have completed execution, the timer's task execution thread terminates gracefully (and becomes subject to garbage collection). However, this can take arbitrarily long to occur.

tangens
thanks alot!!! :)
ufk
If you run that code, you'll leak threads. Make sure to clean up the timer when you've finished.
skaffman
@skaffman: I added a statement from the javadoc. Do you really have to clean up after calling schedule?
tangens
It might be OK, but then it might not be. If you run that code fragment multiple times, you'll have loose threads kicking about with no means of tidying them up.
skaffman
`import java.util.Timer; import java.util.TimerTask;` might make it more obvious that this is not `javax.swing.Timer`. / Note, if you are using Swing (and actually AWT) you shouldn't be doing anything to change components on non-Event Dispatch Thread (EDT) threads (`java.util.Timer` tasks bad; `javax.swing.Timer` actions good).
Tom Hawtin - tackline
Thanks @tom-hawtin-tackline, now I refer fully qualified to the classes.
tangens
+6  A: 

Something like this:

// When your program starts up
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();

// then, when you want to schedule a task
Runnable task = ....    
executor.schedule(task, 5, TimeUnit.SECONDS);

// and finally, when your program wants to exit
executor.shutdown();

There are various other factory methods on Executor which you can use instead, if you want more threads in the pool.

And remember, it's important to shutdown the executor when you've finished. The shutdown() method will cleanly shut down the thread pool when the last task has completed, and will block until this happens. shutdownNow() will terminate the thread pool immediately.

skaffman
+1  A: 

Hi, you could use the Thread.Sleep() function

Thread.sleep(4000);
myfunction();

Your function will execute after 4 seconds. However this will pause the entire program...

dale
It will pause the current thread, not the whole program.
skaffman
Yeah, ofcourse :)
dale
And it only guarantees that the execution will run after 4sec, which could mean after 10 sec as well!
questzen
+1  A: 

Your original question mentions the "Swing Timer". If in fact your question is related to SWing, then you should be using the Swing Timer and NOT the util.Timer.

Read the section from the Swing tutorial on "How to Use Timers" for more information.

camickr