开发者

Synchronization in J2ME

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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜