Results 1 to 7 of 7
Thread: [SOLVED] Timer help
- 03-10-2009, 11:26 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 63
- Rep Power
- 0
[SOLVED] Timer help
Hi, I need help with the Timer Task. I have posted code below. Basically, when i click the menu, various instructions are executed at certain intervals. So, for example, instructions would execute after 5 seconds, then 10 seconds later etc.
Note: When I click the menu, instructions should execute after 5 seconds, then 15 seconds as in the example above.
Help is greatly appreciated.
//----------------------------------------------------------------------
import java.util.*;
int numberOfMillisecondsInTheFuture1 = 5000; // 5 sec
Date timeToRun1 = new Date(System.currentTimeMillis()+numberOfMillisecon dsInTheFuture1);
int numberOfMillisecondsInTheFuture2 = 15000; // 15 sec
Date timeToRun2 = new Date(System.currentTimeMillis()+numberOfMillisecon dsInTheFuture2);
Timer timer = new Timer();
menu1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
timer.schedule(new TimerTask()
{
public void run()
{
System.out.println("Hello");
}
}, timeToRun1);
timer.schedule(new TimerTask()
{
public void run()
{
System.out.println("Hello2");
}
}, timeToRun2);
}
});
- 03-10-2009, 11:41 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
I'm sorry, but, okay, your question/problem is?
- 03-10-2009, 11:50 AM #3
Member
- Join Date
- Oct 2008
- Posts
- 63
- Rep Power
- 0
it doesn't do what I want it to do as I said in the last post. Any ideas?
- 03-10-2009, 11:53 AM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
- 03-10-2009, 12:01 PM #5
Member
- Join Date
- Oct 2008
- Posts
- 63
- Rep Power
- 0
No, I don't get any compiler messages/errors. What happens is it doesn't wait the number of seconds I want it to wait. So, what happens is the first timer executes and then immediately after, the second one executes.
Note that it works when used with public static void main(String [] args), but not with the menu.
- 03-10-2009, 12:33 PM #6
Member
- Join Date
- Oct 2008
- Posts
- 63
- Rep Power
- 0
I tried something else and it works now. :cool:
- 03-10-2009, 12:42 PM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
with that exact code?
should complain that timer is not final.Java Code:void whateverMethod() { Timer timer = new Timer(); menu1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { timer.schedule(new TimerTask() { public void run() { System.out.println("Hello"); } }, timeToRun1); timer.schedule(new TimerTask() { public void run() { System.out.println("Hello2"); } }, timeToRun2); } }); }
It would really help if you posted code using code tags, and made sure that the code was compilable while still displaying the problem.
What happens is it doesn't wait the number of seconds I want it to wait. So, what happens is the first timer executes and then immediately after, the second one executes.
Note that it works when used with public static void main(String [] args), but not with the menu.
Similar Threads
-
Best way to implement a timer
By Nuluvius in forum New To JavaReplies: 11Last Post: 01-16-2009, 01:27 AM -
EJB Timer
By mrjunsy in forum Advanced JavaReplies: 0Last Post: 08-22-2008, 04:09 PM -
EJB Timer
By mrjunsy in forum New To JavaReplies: 0Last Post: 08-04-2008, 06:47 PM -
How to cancel an individual timer in spite of canceling whole timer
By Java Tip in forum java.utilReplies: 0Last Post: 04-04-2008, 02:46 PM -
problem with timer
By Marcus in forum Advanced JavaReplies: 2Last Post: 07-01-2007, 05:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks