11011110110011011001100110011001000110011001100111010001011

March 24, 2007

java.util.Timer, diffs in J2ME & J2SE

Filed under: Programming

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:

Comments »

The URI to TrackBack this entry is: http://lyo.blogsome.com/2007/03/24/javautiltimer-diffs-in-j2me-j2se/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.