Results 1 to 20 of 24
- 08-20-2010, 05:16 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 25
- Rep Power
- 0
Countdown Timer that does not disrupt normal operation
Hi all,
I was hoping to get some assistance with a problem.
I am trying to develop a countdown timer that lasts for 24 hours.
When i make a call to a certain method within my class, i need the countdown to be fired off.
Also, i need the countdown timer to run in the background, so that other calls to the class can occur without having to wait for the timer to finish, and also not disrupt the timer and vice versa.
Im thinking the timer has to be a seperate class. And when the timer fmethod is accessed, a new object of this class is created.
If the timer method is called multiple times within 24 hours, new instances of the objects are created for each of the calls based on numeric iteration.
Am i heading down the right path?
Any ideas?
Thanks.
colpwd
-
Have you looked at using either java.util.Timer or if a Swing app javax.swing.Timer?
- 08-23-2010, 12:46 AM #3
Member
- Join Date
- Aug 2010
- Posts
- 25
- Rep Power
- 0
thankyou, just what i needed.
- 08-23-2010, 01:45 AM #4
Member
- Join Date
- Aug 2010
- Posts
- 25
- Rep Power
- 0
Just to get a bit of extra help with this.
I am using a timer and timertask as follows,
Timer time = new Timer();
TimerTask task = new TimerTask() {
public void run(){
sessionIdKeeper.remove(1);
}
};
And calling this task like this,
time.schedule(task, 10000);
I want to pass through an integer variable which communicates to the task which element of the arraylist to remove (instead of hardcoded value 1).
Is it possible to do this?
Thanks.
-
Sure, just pass a class field into the remove method.
- 08-23-2010, 02:17 AM #6
Member
- Join Date
- Aug 2010
- Posts
- 25
- Rep Power
- 0
Thanks for the reply.
It seems i had to make my integer a global static variable to make this work.
Is this always the case?
-
- 08-23-2010, 07:23 AM #8
Member
- Join Date
- Aug 2010
- Posts
- 25
- Rep Power
- 0
Update:
Ok, so i worked out i have to use an ArrayList of Timer type. That should help.
So everytime that methods gets called it will create a new instance of the object in the arraylist.Java Code:ArrayList<Timer> timerKeeper = new ArrayList<Timer>(); TimerTask task = new TimerTask() { public void run() { sessionIdKeeper.remove(myClass.flag); } }; public void timerStart() { timerKeeper.add(new Timer()); timerKeeper.get(flag).schedule(task, 10000); }
My problem now is at the moment, the sessionIdKeeper arraylist just keeps growing without the remove function acting upon elements older than 10 seconds.Last edited by colpwd; 08-23-2010 at 11:53 PM.
-
I think you would be best served by creating an SSCCE that we can run and test. To see how to do this, please read the 3rd link in my signature below. It will also help greatly if you use code tags when posting code (check out my first signature link for info on this).
Luck.
- 08-24-2010, 12:35 AM #10
Member
- Join Date
- Aug 2010
- Posts
- 25
- Rep Power
- 0
Hi Fubarable,
I am sorry for not using coding tags.
I have created a small program that is modelling what i am trying to do.
Java Code:/** The objective here is to create an ArrayList of Timer objects. ** When timerCall() is called, a Timer object is added to the ArrayList and begins its countdown from 10 seconds. When 10 seconds elapses for ** this specific object, the related TimerTask object is called and deletes an element of the sessionIdKeeper ArrayList. ** Elements are added to sessionIdKeeper each time timerCall() is called. **/ import java.util.*; public class youngbloods{ public static int flag = 0; //Declare flag to keep track of timer objects public int own = 0; public static ArrayList<String> sessionIdKeeper = new ArrayList<String>(); public static ArrayList<Timer> timerKeeper = new ArrayList<Timer>(); public static ArrayList<TimerTask> taskKeeper = new ArrayList<TimerTask>(); class bryan extends TimerTask{ public void run() { System.out.println("int variable equal to index to delete from arraylist: " + own); System.out.println("size of arraylist: " + sessionIdKeeper.size()); System.out.println("lastIndexOf 123 in the arraylist: " + sessionIdKeeper.lastIndexOf("123")); //System.out.println(sessionIdKeeper.get(2)); sessionIdKeeper.remove(own); own++; //Code to execute as part of TimerTask. } } public static void main(String args[]) { youngbloods something = new youngbloods(); //System.out.println(flag); //System.out.println(timerKeeper.size()); // System.out.println(sessionIdKeeper.size()); something.timerCall(); something.sleep(); //System.out.println(flag); //System.out.println(timerKeeper.size()); //System.out.println(sessionIdKeeper.size()); something.timerCall(); something.sleep(); //System.out.println(flag); //System.out.println(timerKeeper.size()); // System.out.println(sessionIdKeeper.size()); something.timerCall(); //3 Timer objects added to ArrayList System.out.println(sessionIdKeeper.get(0)); System.out.println(sessionIdKeeper.get(1)); System.out.println(sessionIdKeeper.get(2)); //System.out.println(flag); //System.out.println(timerKeeper.size()); //System.out.println(sessionIdKeeper.size()); } public void timerCall() { //bryan task = new bryan(); sessionIdKeeper.add("123"); timerKeeper.add(new Timer()); taskKeeper.add(new bryan()); System.out.println(flag); //Add a Timer object to the array list timerKeeper.get(flag).schedule(taskKeeper.get(flag), 10000); flag++; System.out.println(flag); } public void sleep() { try { Thread.sleep(1000); // do nothing for 1000 miliseconds (1 second) } catch(InterruptedException e) {} } }
This compiles, although its producing an error which refers to the Timer object already being scheduled.
Thanks a lot for your help,
colpwdLast edited by colpwd; 08-24-2010 at 11:37 PM.
- 08-24-2010, 01:55 AM #11
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
I have'nt use Task or timerTask and I am new to java but I think it is a little same with SwingWorker.
In SwingWorker you have to renew the Task everytime you call it because you cannot recycle it.
just guess.Java Code:aSwingWorker ThisIsASwingWorker = new aSwingWorker(); ThisIsASwingWorker.execute();
- 08-24-2010, 02:20 AM #12
Try debugging your code to see what is happening by breaking this statement up into separate steps and printing out the results of each step:
timerKeeper.get(flag).schedule(task, 10000);
for example:
Timer txx = timerKeeper.get(flag);
System.out.println("txx=" + txx + ", flag=" + flag);
- 08-24-2010, 02:24 AM #13
Member
- Join Date
- Aug 2010
- Posts
- 25
- Rep Power
- 0
Thanks for the reply,
I know the problem lies with using the same TimerTask for different instances of the Timer object.
I am just not sure how to create a seperate TimerTask for each Timer instance created.
Trying to add a TimerTask to an ArrayList like i have a Timer instance yields the "TimerTask is abstract; cannot be instantiated" error
Searching around for a nice way to do this.
Thanks,
colpwd
-
- 08-24-2010, 02:54 AM #15
Extend the class and override the run() method."TimerTask is abstract; cannot be instantiated"
- 08-24-2010, 04:04 AM #16
Member
- Join Date
- Aug 2010
- Posts
- 25
- Rep Power
- 0
I am so close now, thanks for all your help.
I have updated my code above, I am now seeing some strange behaviour with the remove function of ArrayList.
Instead of deleting the element equal to the value of my flag-like variable called "own", it removes the last element of the ArrayList?
I get the following output only to the run() method above.
P.S The Timer/TimerTask is working well.
Java Code:0 3 2 1 2 1 2 1 0 Exception in thread "Timer-2" java.lang.IndexOutOfBoundsException: Index: 2, Siz e: 1 at java.util.ArrayList.RangeCheck(ArrayList.java:547) at java.util.ArrayList.remove(ArrayList.java:387) at youngbloods$bryan.run(youngbloods.java:24) at java.util.TimerThread.mainLoop(Timer.java:512) at java.util.TimerThread.run(Timer.java:462)
- 08-24-2010, 05:30 AM #17
Member
- Join Date
- Aug 2010
- Posts
- 25
- Rep Power
- 0
Ok, so it appears that the ArrayList is "auto trimming" when i delete an element, so the element i want to remove is always in position 0.
I'd like to thank everyone for your help.
colpwd
- 08-24-2010, 01:48 PM #18
What do these numbers show? Current value of item being deleted? The size of the list ???0
3
2
1
2
1
2
1
0
Its almost useless to print them without ids on them.
Add an id on the print to show value:
println("flag=" + flag);
and
println('own1= " + own);
and
System.out.println("tK.size=" + timerKeeper.size());
Add more println()s to show the value of the variables every time they are changed with an id that tells who/where they are being changed. For example:
println("flag1= + flag);
and
println"flag2=" + flag);
- 08-24-2010, 11:33 PM #19
Member
- Join Date
- Aug 2010
- Posts
- 25
- Rep Power
- 0
Sorry about not labelling the numbers, i am so used to the code now that i just know the order in which the numbers appear.
I have managed to come up with a solution.
I have updated the code though, with labelling, if your still interested.
It seems as though the arraylist auto trims or does a left shift operation after an element is removed. This can be seen by paying attention to the lastIndexOf value, which should remain the same (value of 2) if i am deleting elements below 2. Yet it decreases from 2-1-0.
If i populate the 0th, 1st and 2nd elements of an array, and then delete the 0th element, the 1st and 2nd elements stay intact correct?
Setting the remove function to always act upon the 0th element solves this problem, and hence no flag is required to keep track of the next apporporiate element to delete in the ArrayList.Last edited by colpwd; 08-24-2010 at 11:46 PM.
- 08-24-2010, 11:54 PM #20
Similar Threads
-
Is it normal to have these feelings?
By Meta in forum New To JavaReplies: 15Last Post: 05-03-2010, 09:20 AM -
how to countdown on server side
By BigBear in forum Java ServletReplies: 3Last Post: 04-26-2010, 10:33 PM -
countdown timer, little help with method
By sidy in forum New To JavaReplies: 22Last Post: 07-19-2008, 12:42 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 -
CountDown timer
By Seema Sharma in forum AWT / SwingReplies: 1Last Post: 03-06-2008, 04:26 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks