例如,一个类有一个私有数据库和一个公共方法 - put
。我们有几个线程要运行put
,这样可以吗?(ps:我们不考虑 k/v 的顺序)
void put(List<byte[] keys, List<byte[]> values) {
WriteOptions writeOpt = new WriteOptions();
WriteBatch batch = new WriteBatch();
for (int i = 0; i < keys.size(); ++i) {
batch.put(keys.get(i), values.get(i));
}
this.db.write(writeOpt, batch);
}
这里的医生说
However other objects (like Iterator and WriteBatch) may require external synchronization.
If two threads share such an object, they must protect access to it using their own locking protocol.
但是对于上面的示例,线程不共享相同的WriteBatch
,它们拥有自己的WriteBatch
,并写入数据库。所以我想知道这样可以吗?