Java Timer: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
No edit summary
Line 13: Line 13:


<blockquote style="background-color: Gold; border: solid thin Goldenrod;">
<blockquote style="background-color: Gold; border: solid thin Goldenrod;">
:Note that if the <tt>TimerTask.run()</tt> implementation throws an unchecked exception, the Timer is canceled - there is no try/catch logic inside the timer's main loop.
:<br>Note that if the <tt>TimerTask.run()</tt> implementation throws an unchecked exception, the Timer is canceled - there is no try/catch logic inside the timer's main loop.<br><br>
<br>
</blockquote>
</blockquote>



Revision as of 02:49, 31 July 2016

External

Internal

Overview

The Java java.util.Timer allows a task to be scheduled for execution at a specified time, or with a fixed delay, or at a fixed rate.


Note that if the TimerTask.run() implementation throws an unchecked exception, the Timer is canceled - there is no try/catch logic inside the timer's main loop.

class TimerThread extends Thread {
    ...
    private void mainLoop() {
        while (true) {
            try {
                ...
                if (taskFired)  // Task fired; run it, holding no locks
                    task.run();
            } catch(InterruptedException e) {
            }
        }
    }
   ...
}