Page 1 of 2 12 LastLast
Results 1 to 20 of 26
  1. #1
    colpwd is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    colpwd is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default

    thankyou, just what i needed.

  4. #4
    colpwd is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default

    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.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  6. #6
    colpwd is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default

    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?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by colpwd View Post
    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?
    If it is contained within a static method, then yes, this makes sense. Otherwise if the method that holds it is an instance method, then a class-level instance variable or method-level final variable should work fine.

  8. #8
    colpwd is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default

    Update:

    Ok, so i worked out i have to use an ArrayList of Timer type. That should help.
    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);
    
     }
    So everytime that methods gets called it will create a new instance of the object in the arraylist.
    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.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  10. #10
    colpwd is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default

    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,

    colpwd
    Last edited by colpwd; 08-24-2010 at 11:37 PM.

  11. #11
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    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.
    Java Code:
        aSwingWorker ThisIsASwingWorker = new aSwingWorker();
        ThisIsASwingWorker.execute();
    just guess.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    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);

  13. #13
    colpwd is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default

    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

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by mine0926 View Post
    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.
    There's been no indication that this is a Swing issue, and if it were, you'd want to use a Swing Timer, not a Swing worker.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    "TimerTask is abstract; cannot be instantiated"
    Extend the class and override the run() method.

  16. #16
    colpwd is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default

    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)

  17. #17
    colpwd is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default

    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

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    0
    3
    2
    1
    2
    1
    2
    1
    0
    What do these numbers show? Current value of item being deleted? The size of the list ???
    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);

  19. #19
    colpwd is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default

    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.

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    seems as though the arraylist auto trims
    Would you expect it to have holes where items have been removed?

    To keep items in the same order in a list, start at the end and move towards the front.
    Then you are NOT changing the indexes of any of the items.

Page 1 of 2 12 LastLast

Similar Threads

  1. Is it normal to have these feelings?
    By Meta in forum New To Java
    Replies: 15
    Last Post: 05-03-2010, 09:20 AM
  2. how to countdown on server side
    By BigBear in forum Java Servlet
    Replies: 3
    Last Post: 04-26-2010, 10:33 PM
  3. countdown timer, little help with method
    By sidy in forum New To Java
    Replies: 22
    Last Post: 07-19-2008, 12:42 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:46 PM
  5. CountDown timer
    By Seema Sharma in forum AWT / Swing
    Replies: 1
    Last Post: 03-06-2008, 04:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •