java.util.Timer, diffs in J2ME & J2SE
I find out the diffs from the game 3DNeedForDrift, it used Timers to call Canvas.repaint().
It causes an Nullexception in Timer.run()->mainloop()->canvas.repaint(), there is one frame with NULL canvas.
Let’s see the differences of Timer’s implement:
[J2ME/MIDP2.0]
class TimerThread extends Thread {
…….
public void run() {
try {
mainLoop();
} catch (Throwable t) {
// Someone killed this Thread, behave as if Timer cancelled
synchronized (queue) {
newTasksMayBeScheduled = false;
queue.clear(); // Eliminate obsolete references
}
}
}
private void mainLoop() {
while (true) {
try {
……..
if (taskFired) { // Task fired; run it, holding no locks
try {
task.run();
} catch (Exception e) {
// Cancel tasks that cause exceptions
task.cancel();
}
}
} catch (InterruptedException e) {
}
}
}
}
[J2SE/1.4.2]
class TimerThread extends Thread {
…….
public void run() {
try {
mainLoop();
} finally {
// Somone killed this Thread, behave as if Timer cancelled
synchronized(queue) {
newTasksMayBeScheduled = false;
queue.clear(); // Eliminate obsolete references
}
}
}
private void mainLoop() {
while (true) {
try {
……..
if (taskFired) // Task fired; run it, holding no locks
task.run();
} catch (InterruptedException e) {
}
}
}
}
===================
In J2ME, the try…catch… block makes the Timer running normally even if the task throws an exception.
But in J2SE, there is none protection, it’s why the 3DNeedForDrift failed running in KEmulator!
crack rt.jar to run the game: