阅读 RecordStore documentation ,可以阅读以下内容:
记录存储实现确保所有单独的记录存储操作都是原子的、同步的和序列化的,因此多次访问不会发生损坏。
然后在下一句:
但是,如果 MIDlet 使用多个线程访问记录存储,则 MIDlet 有责任协调此访问,否则可能会导致意想不到的后果。
我不知道该怎么做。这些操作是原子的和同步的,但我必须自己同步访问?假设我不需要迭代记录,我需要什么类型的同步来实现简单的添加/更新/删除功能?
如果我确实需要迭代,如果我使用 RecordEnumeration
,enumerateRecords
方法是否获取 RecordStore
的快照,或者它直接连接到它吗?
最佳答案
However, if a MIDlet uses multiple threads to access a record store, it is the MIDlet's responsibility to coordinate this access or unintended consequences may result.
我认为这意味着两个线程同时读取和写入存储中的记录可能会产生不良结果;特别是当您依赖于特定顺序的商店,或者记录具有已知索引时。
您的第二个问题在 RecordStore.enumerateRecords()
的 Javadoc 中得到了回答。 :
keepUpdated
- if true, the enumerator will keep its enumeration current with any changes in the records of the record store. Use with caution as there are possible performance consequences. If false the enumeration will not be kept current and may return recordIds for records that have been deleted or miss records that are added later. It may also return records out of order that have been modified after the enumeration was built. Note that any changes to records in the record store are accurately reflected when the record is later retrieved, either directly or through the enumeration. The thing that is risked by setting this parameter false is the filtering and sorting order of the enumeration when records are modified, added, or deleted.
关于java-me - 了解 J2ME 中 RecordStore 的同步需求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9008499/