Results 1 to 6 of 6
- 04-02-2011, 07:33 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
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
-
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.
-
You could use Timer's scheduleAtFixedRate to repeat things every 1000 ms.
-
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.
-
Cross-post: Java. Simple TimerTask for each value of array - Stack Overflow
Please read this link regarding cross-posts: BeForthrightWhenCrossPostingToOtherSites
Then please acknowledge that you understand this issue and will abide by its recommendations.
- 04-02-2011, 09:16 PM #6
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
thread + timertask
By anra in forum Threads and SynchronizationReplies: 0Last Post: 03-08-2011, 10:05 AM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
hibernate session = null in the run() method of TimerTask
By fabs in forum Threads and SynchronizationReplies: 1Last Post: 09-17-2009, 03:12 PM -
Timer and TimerTask
By AndrewM16921 in forum New To JavaReplies: 2Last Post: 04-07-2009, 11:40 PM -
TimerTask with a Date_Time
By kunta in forum Sun Java Wireless ToolkitReplies: 1Last Post: 05-31-2007, 10:17 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks