views:

22

answers:

1

If one doesn't use Threads or Timers, they wouldn't need synch, as all input/output is handled by a single thread. However, if one introduces TimerTasks, synchronization would be mandatory.

There are two ways to synch the code in J2ME:

  1. The usual: using locks
  2. Using Display.callSerially(Runnable r) so that all external events would be synched with the Event Thread.

The question is: which way is better or, at least, more widely used? And secondly: if the second way is the preferred one, is the following implementation, reasonable?

class MyTimerTask extends TimerTask {
  Display display;
  RunnableObject r {
      public void run() {
        ...
      }
  }
  ...
  public void run() {
    display.callSerially(r);
  }
}

Thanks!

A: 

I prefer the second one, it is more clear to me. I can't see anything wrong with your implementation, I think you can use it safely.

Mahdi Hijazi
Thanks. Well, actually I though about it *quite* a bit. The problem is that calling serviceRepaints() from code running on the Event Thread causes deadlock. Best way I found, is using ONE other thread, an locking on it. It's simple and *more* flexible. Thank again!
Albus Dumbledore
you don't need to lock the serviceRepaint, because it will not causes any deadlock unless you hold a lock a acquired by the paint method, which you must a void.
Mahdi Hijazi