我有一个对象负责持久化JTable状态到磁盘.它保存/加载可见列,它们的大小,位置等.其类定义中的一些有趣的位如下.
class TableSaver {
Timer timer = new Timer(true);
TableSaver() {
timer.schedule(new TableSaverTimerTask(),15000,SAVE_STATE_PERIOD);
}
synchronized TableColumns load(PersistentTable table) {
String xml = loadFile(table.getTableKey());
// parse XML,return
}
synchronized void save(String key,TableColumns value) {
try {
// Some preparations
writeFile(app.getTableConfigFileName(key),xml);
} catch (Exception e) {
// ... handle
}
}
private class TableSaverTimerTask extends TimerTask {
@Override
public void run() {
synchronized (TableSaver.this) {
Iterator iterator = queue.iterator();
while (iterator.hasNext()) {
PersistentTable table = iterator.next();
if (table.getTableKey() != null) {
save(table.getTableKey(),dumpState(table));
}
iterator.remove();
}
}
}
}
}
>只存在一个TableSaver实例.
> load()可以从很多线程调用.计时器显然是另一个线程.
> loadFile()和writeFile()不会留下打开的文件流 – 它们使用一个健壮,经过良好测试和广泛使用的库,最终通过try …关闭流.
有时这样会失败,例如:
java.lang.RuntimeException: java.io.FileNotFoundException: C:\path\to\table-MyTable.xml (The requested operation cannot be performed on a file with a user-mapped section open)
at package.FileUtil.writeFile(FileUtil.java:33)
at package.TableSaver.save(TableSaver.java:175)
at package.TableSaver.access$600(TableSaver.java:34)
at package.TableSaver$TableSaverTimerTask.run(TableSaver.java:246)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)
Caused by: java.io.FileNotFoundException: C:\path\to\table-MyTable.xml (The requested operation cannot be performed on a file with a user-mapped section open)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.(Unknown Source)
at java.io.FileOutputStream.(Unknown Source)
at package.FileUtilWorker.writeFile(FileUtilWorker.java:57)
... 6 more
所以我有两个问题:
>这种同步怎么会失败?请注意,我确定只有一个TableSaver实例.
> stacktrace中的这些东西是什么?package.TableSaver.access $600(TableSaver.java:34)?第34行是与类TableSaver {的行.这可能是同步不行的原因吗?