简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM