Click here to Skip to main content
14,733,599 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello there,
I want to ask you people a question. Can a java timer be initialized in a for loop? So that the timer will once run once with a value taken from an ArrayList. Something like the idea bellow:

  void myTimerMethod(){

   int size=ArrayList.getSize();
   Timer timer=new Timer();

   for(int i=0;i>size;i++){

     timer.schedule(new TimerTask(){

         public void run(){

           //do something here for a specified amount of time.
           //let's say display a character in console.


     },ArrayList.get(i));  //set the timer to run only once, for a value taken from ArrayList.



I want to know if something like this can be done. If yes, could you please give me an advice on how to implement such a method :) ?...

Thank you very much.
Prasad Khandekar 30-Apr-13 6:49am

It's essentially going to create those many scheduled tasks. Please note that the second parameter to schedule method the delay after which the task will start executing. It's not going to be the value sent to the TimetTask. Also note that all these tasks will run sequentially by the background thread associated with the timer object. If your taks are long runnning then this may not be the good idea.

nameJulian 30-Apr-13 6:55am
What i basically want is...for different time values, to send a character to COM port with RXTX library. Thank you for the opinion.

1 solution

your are initalizing the Timer object again and again!

Take it completly into the loop.

I would also extend it with a custom object adding a name/identifier attribute to the Timer:

public class MyTimer extends Timer{

  private final strID; // final -> has to be set by constructor

  public MyTimer(String strID){
    this.strID = strID;

  public String getID(){
    return strID;

So you can identifify the Timer later on during runtime.
A number would do also fine as ID.
nameJulian 30-Apr-13 10:23am
I have thought of what you said about the timer to be taken completly into the loop. I have done so. The problem is the next thing: if i have in the array lets say, the number 4 and 8. At the moment, the code does something like this: it start counting, by the time it reaches 4, the program does something ( System.out.println("reached here") - in my code ), after that it continues counting until it reaches 8. Here again the program does something.
What i need is that after it reached 4, the timer stops, it takes the value 8 and starts counting again from 0 until it reaches 8.

Or better said, after the first timer reaches 4, it stops counting and the second timer starts counting until it reaches 8, then it stops.

Hope you understand.
TorstenH. 2-May-13 4:49am
So you want to react on the one timer ending and start another one then...
You need to be able to address the timers (as I said they need an ID) and you need to schedule them:, long)
nameJulian 3-May-13 9:46am
Does it matter if i use java.util.Timer or javax.swing.Timer?

I know that when the current timer stops, the second one must be scheduled from the first one ( from the firsts one run() method ). I know that using too many timers is bad, but i strongly need this. Thank you.
TorstenH. 6-May-13 5:31am
javax.swing.Timer is a gui component. Totally different purpose.
nameJulian 6-May-13 5:46am
I have solved my problem. I stopped using timers and moved to using threads. The problem has been solved, but the idea that you gave me is good as well. Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900