使用 Java Swing Worker 作为单例在后台备份应用程序数据
我计划使用 Java SwingWorker 作为单例来定期在后台备份应用程序数据(也许与 Swing 计时器或 java.util.Timer 结合使用)
这样的策略合理吗?将 Singleton 应用于 SwingWorker 是一个好主意吗?
I plan to use Java SwingWorker as a singleton to backup application data in the background periodically(perhaps in combination with a Swing timer or java.util.Timer)
is such a strategy sound? is applying Singleton to SwingWorker a good idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不明白
SwingWorker
与备份任务有什么关系。我也不明白单例模式有什么关系。这是计时器的任务。然而,您可能必须小心不要在多个线程中启动它,但这应该是可行的,而无需将其包装为单例。I don't see what
SwingWorker
has to do with a backup task. I don't see either what the singleton pattern has to do with. This is a task for aTimer
. You might have to be careful not to start it in multiple threads however, but that should be doable without wrapping it as a singleton.我认为您可以轻松使用
SwingWorker
来保存模型的状态。如果您想在后台执行此任务,最好是制作状态的当前副本并保存副本,因此您无需担心在另一个线程中保存模型时同步模型的状态。在进行恢复时,您可能希望确保用户不会在从先前版本的模型恢复期间执行并随后丢失他所做的所有操作。当成功恢复时,您可能会将当前状态设置为它。因此,在这种情况下,阻止任何用户输入甚至是合适的,这样您就可以轻松地在当前线程上执行此操作。首先向您的用户显示一些适当的信息/对话框,表明状态正在恢复。
I think you could easily use the
SwingWorker
for saving the state of your model. If you want to do this task in the background preferably would be to make a current copy of your state and save the copy thus you do not need to worry about synchronizing the state of the model while you are saving it in another thread.Where it goes to recovery you probably want to make sure that user will not make and later loose all that he did during the model recovery from a previous version. As on successful recovery you probably will set the current state to it. Thus in this case it is even appropriate to block any user input thus you can easily do it on the current thread. Firstly displaying some appropriate information/dialog to you user that the state is being recovered.