使用 Java Swing Worker 作为单例在后台备份应用程序数据

发布于 2024-11-10 09:26:53 字数 135 浏览 2 评论 0原文

我计划使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅的被狗咬 2024-11-17 09:26:53

我不明白 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 a Timer. 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.

葮薆情 2024-11-17 09:26:53

我认为您可以轻松使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文