-3

Possible Duplicate:
Timer Won't Fire Correctly

Apparently, in Java 1.6, the Timer doesn't work like it used to, so how do I get a task to fire every 250ms (1/4 of a second)?

Community
  • 1
  • 1
Cg2916
  • 1,117
  • 6
  • 23
  • 36
  • 2
    [`java.util.Timer`](http://docs.oracle.com/javase/7/docs/api/java/util/Timer.html) or [`javax.swing.Timer`](http://docs.oracle.com/javase/7/docs/api/javax/swing/Timer.html)? – mre Nov 27 '11 at 04:58
  • 6
    What do you mean by *it doesn't work like it used to*? The API looks pretty much the same to me. You can still schedule tasks for fixed interval execution with Java6. Is there some specific behaviour that you need that's not in Java6 API? – rodion Nov 27 '11 at 05:03
  • 3
    -1: wtf? I hate leaving flyby downvotes but I really don't know what to say here. You really think they just disabled timers in 1.6? Any information source? Have you looked through the APIs? Where is your code sample--what do you currently have? Give us SOMETHING to work with here. – Bill K Nov 27 '11 at 05:38
  • My problem was that i was using the Swing timer instead of the java.util.Timer. – Cg2916 Nov 27 '11 at 14:40
  • Yeah, it's firing as fast as it can instead of every 250ms. – Cg2916 Nov 27 '11 at 16:08

1 Answers1

1

If you want to do task every 250ms even doStuff() may take more than 250ms, you should use a new thread to "doStuff"(In this case, more than one doStuff may work at a time)

updated(I tried this in win7x64, JDK 1.6 and it works)

    java.util.TimerTask task = new java.util.TimerTask() {
        @Override
        public void run() {
            System.out.println("yoo");
        }
    };
    java.util.Timer timer = new java.util.Timer();
    timer.schedule(task, java.util.Calendar.getInstance().getTime(), 250);
Kanglai
  • 530
  • 1
  • 6
  • 18