44 |
* it should have been scheduled and cancel itself when no longer needed. |
* it should have been scheduled and cancel itself when no longer needed. |
45 |
* <p> |
* <p> |
46 |
* Example: |
* Example: |
47 |
* <code> |
* <pre> |
48 |
* Timer timer = new Timer(); |
* Timer timer = new Timer(); |
49 |
* TimerTask task = new TimerTask() { |
* TimerTask task = new TimerTask() { |
50 |
* public void run() { |
* public void run() { |
51 |
* if (this.scheduledExecutionTime() < System.currentTimeMillis() + 500) |
* if (this.scheduledExecutionTime() < System.currentTimeMillis() + 500) |
52 |
* // Do something |
* // Do something |
53 |
* else |
* else |
54 |
* // Complain: We are more then half a second late! |
* // Complain: We are more then half a second late! |
56 |
* this.cancel(); // This was our last execution |
* this.cancel(); // This was our last execution |
57 |
* }; |
* }; |
58 |
* timer.scheduleAtFixedRate(task, 1000, 1000); // schedule every second |
* timer.scheduleAtFixedRate(task, 1000, 1000); // schedule every second |
59 |
* </code> |
* </pre> |
60 |
* <p> |
* <p> |
61 |
* Note that a TimerTask object is a one shot object and can only given once |
* Note that a TimerTask object is a one shot object and can only given once |
62 |
* to a Timer. (The Timer will use the TimerTask object for bookkeeping, |
* to a Timer. (The Timer will use the TimerTask object for bookkeeping, |