Results 1 to 6 of 6
  1. #1
    lexikovs is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Question TimerTask for each value of array

    Hello.

    I have problem with printing out the each value of certain array with certain duration of time.
    For example I have array with values: "Value1", "Value2", "Value3". I want to output "Value1", after 5 sec "Value2", after 5 second "Value3".
    Instead, All Values of arrays are printout 3 times.
    If you could help to me, I will be so gratefull ))
    Thank you.


    Here is my code.
    --------------------

    Java Code:
    import java.util.Date;
    
    public class Timer2 {
    
    	/**
    	 * @param args
    	 */
    	  public static void main(String[] args) { 
    		  
    	        long start = new Date().getTime(); 
    	        
    	     
    	        
    	       for(int i=0; i<4; i++){ 
    	            new java.util.Timer().schedule(new java.util.TimerTask(){ 
    	            	
    	                public void run(){ 
    	                	String[] arrayElements = {"value1", "value2", "value3", "value4"};
    	                	 for (int i=0; i<arrayElements.length; i++ )
    	                    System.out.println(arrayElements[i]); 
    	                	 
    	                } 
    	            },
    	            new Date(start)); 
    	           start+=1000; 
    	        } 
    	    }
    
    }
    Last edited by Fubarable; 04-02-2011 at 07:43 PM. Reason: code tags added

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Here are some steps to help you out:

    Java Code:
    1. get the time
    2. calculate the time the task ends i.e. original time + (nOf tasks x 5seconds)
    3.i. loop forever
    i.e.
    boolean timePassed = false;
    while (timePassed==false) {
        //loop
        3.ii. if time up, timePassed = true;
    }
    4. set loop condition
    i.e.
    //loop
    int stepCount = 1;
    if (new timeInstance-originalTime == stepCount*5 seconds) {
        //perform action
        ++stepCount;
    }
    5. set time up condition
    if (new timeInstance >= calculation answer from 2.) timePassed = true;

    to simplifiy time calculations, use long Calendar.getTimeInMillis();
    Last edited by ozzyman; 04-02-2011 at 07:47 PM.

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

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

    Default

    To ozzyman: why not use java.util.Timer and TimerTask?

    Much as I hate to give full solutions, this one is brief and is in comparison with your recommendations:

    Java Code:
    import java.util.Timer;
    import java.util.TimerTask;
    
    class Timer2 {
    
       private static final String[] ARRAY_ELEMENTS = {"value1", "value2", "value3", "value4"};
    
       public static void main(String[] args) {
          final Timer utilTimer = new Timer();
          utilTimer.scheduleAtFixedRate(new TimerTask() {
             private int index = 0;
    
             public void run() {
                System.out.println(ARRAY_ELEMENTS[index]);
                index++;
                if (index >= ARRAY_ELEMENTS.length) {
                   utilTimer.cancel();
                }
             }
          }, 1000L, 1000L);
       }
    
    }
    Last edited by Fubarable; 04-02-2011 at 08:18 PM.

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

  6. #6
    lexikovs is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    The solutions are found. Thank you for your answers.
    Every idea were valuable.
    Thread is closed.
    Last edited by lexikovs; 04-02-2011 at 09:29 PM.

Similar Threads

  1. thread + timertask
    By anra in forum Threads and Synchronization
    Replies: 0
    Last Post: 03-08-2011, 10:05 AM
  2. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  3. hibernate session = null in the run() method of TimerTask
    By fabs in forum Threads and Synchronization
    Replies: 1
    Last Post: 09-17-2009, 03:12 PM
  4. Timer and TimerTask
    By AndrewM16921 in forum New To Java
    Replies: 2
    Last Post: 04-07-2009, 11:40 PM
  5. TimerTask with a Date_Time
    By kunta in forum Sun Java Wireless Toolkit
    Replies: 1
    Last Post: 05-31-2007, 10:17 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
  •