J2ME 中的同步
如果不使用线程或定时器,则它们不需要同步,因为所有输入/输出均由单个线程处理。然而,如果引入 TimerTasks,同步将是强制性的。
在 J2ME 中同步代码有两种方法:
- 通常:使用锁
- 使用 Display.callSerially(Runnable r) 以便所有外部事件都与事件线程同步。
问题是:哪种方式更好,或者至少使用更广泛?其次:如果第二种方式是首选方式,那么以下实现合理吗?
class MyTimerTask extends TimerTask {
Display display;
RunnableObject r {
public void run() {
...
}
}
...
public void run() {
display.callSerially(r);
}
}
谢谢!
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:
- The usual: using locks
- 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我更喜欢第二个,它对我来说更清楚。我看不出你的实现有什么问题,我认为你可以安全地使用它。
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.