一般而言,在 SWING / Java 中使用线程时的最佳实践

发布于 2024-11-19 11:11:54 字数 660 浏览 1 评论 0原文

我有一个 SWING UI,其中包含一个用于创建新 SwingWorker 线程的按钮。然后该线程查询 SQLite 数据库以获取结果并将其放入 JTable 中。在我的 StringWorker 构造函数中,参数是从其他 SWING 组件(例如 JSpinner、JComboBoxes 等)获取的各种字段。

由于我对所有线程事物都是新手,因此我希望了解更多知识渊博的程序员关于我应该如何进行的一些建议去做我想做的事。

  1. 我想知道当我使用 System.exit(0) 关闭程序时线程是否自动结束;所以我不会最终出现内存泄漏

  2. 确保没有两个线程同时访问数据库的最佳方法是什么(假设用户多次单击按钮,或者其他情况) ,管理员正在使用一些文件作为输入更新数据库(在我的程序中),然后当第一个线程解析文件并更新数据库时,他想使用按钮查询数据库等)。

  3. 使用线程会慢吗?起初我在 EDT 中完成了所有计算,当然每次按下按钮后 UI 都会锁定,但如果我没记错的话,它只锁定了大约 5 秒。现在,当我按下按钮时,它不会锁定,但结果似乎需要不到两倍的时间才能显示在 JTable 中。是因为我在代码中犯了错误还是这很正常?

我想在查询所在的类中使用静态字段,并在使用时将其设置为 true 。这是正确的做法吗?这样,无论哪个线程正在使用数据库,第二个线程都不会启动。

I have a SWING UI that contains a button that creates a new SwingWorker thread. That thread then queries the SQLite database for results to put them in a JTable. In my StringWorker constructor, the parameters are various fields taken from other SWING components such as a JSpinner, JComboBoxes, etc.

Since I'm new to all of this thread thing, I'd like some advice from more knowledgeable programmers on how I should go about doing what I want to do.

  1. I'd like to know if threads automatically end when I close the program with System.exit(0); so I don't end up with memory leaks

  2. What is the best way to make sure I don't have two threads accessing my database at the same time (let's say the user clicks multiple times on the button or, other case, an administrator is updating the database with some files as input (within my program), then while the first thread is parsing the files and updating the database, he wants to query the database using the button, etc.).

  3. Is it slower to use threads? At first I did all my calculations right in the EDT and of course the UI locked every time after pressing the button, but it only locked for about 5 seconds if I recall correctly. Now, when I press the button, it doesn't lock up but it seems like the result take about a little bit less than twice as long to show up in the JTable. Is it because I made a mistake in my code or is this normal?

I though about using a static field in the class the queries are in and setting it to true if it's in use. Is that the correct way of doing it? That way, not matter which thread is using the database, the second thread won't launch.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

安穩 2024-11-26 11:11:54
  1. 如果不是绝对必要(也不应该),请不要在代码中使用 System#exit。以下是一些说明为什么以及什么更好。

  2. 您的数据库能够处理两个并发请求,因此这本身并不是一件坏事。如果您通过 DataSource 使用 JDBC 及其池连接,那么您可能应该限制一次对一个线程使用这样一个连接。为了解决冗余数据库查询的问题,例如“单击两次”时,可能有不止一种解决方案。我在这里假设您指的是这样的场景:您有一个分发给多个人的 Swing UI,并且每个实例都与同一个数据库通信 ->只需在数据库查询执行期间禁用按钮即可。

  3. 如果由于工作线程的执行调度而没有直接在事件调度线程中运行代码,速度会稍微慢一些,但这应该不会引起注意。要查看出了什么问题,我必须查看相关代码。

  1. If it's not absolutely necessary (it shouldn't be), don't use System#exit in your code. Here are some explanations why and what is better.

  2. Your database is capable of handling two concurrent requests, so it's not a bad thing in itself. If you use JDBC and its pooled connections via DataSource, then you should probably restrict the usage of one such a connection to one thread at a time. To cure the problem of having redundant database queries, e.g. when "clicking twice", there is probably more than one solution. I assume here that you mean the scenario where you have a Swing UI that is distributed to several people, and each of these instances talks to the same database -> simply disable your button as long as the execution of the database query takes.

  3. It's slightly slower if you do not run your code directly in the Event Dispatch Thread due to scheduling of execution of your workers, but this should not be noticable. To see what goes wrong I would have to see the relevant code.

水波映月 2024-11-26 11:11:54

我想知道当我使用 System.exit(0) 关闭程序时线程是否自动结束;

是的。整个进程和属于该进程的线程将结束。但是,如果您不调用 System.exit(),则所有非守护线程都必须在进程消失之前完成。

确保没有两个线程同时访问数据库的最佳方法是什么

由于它是 Swing 应用程序,我假设您和管理员都无法同时访问该应用程序。然而,为了保证即使在单个应用程序中也无法启动多个影响数据库的操作,您必须阻止 UI。禁用按钮或将玻璃窗格放在 UI 顶部。模态进度对话框也很有帮助。

使用线程会慢吗?

不,如果做得好的话,速度并不会变慢。缓慢的操作将需要很长的时间。您无法使用线程来修复它,但您可以在提供良好的非阻塞 UI 的同时保持(感知)速度相同,或者您可以一次执行多个慢速操作,从而提高感知速度。

I'd like to know if threads automatically end when I close the program with System.exit(0);

Yes. Entire process will end and threads that are part of this process. However, if you don't call System.exit(), all non daemon threads must finish before process is gone.

What is the best way to make sure I don't have two threads accessing my database at the same time

Since it's a Swing application, I assume that both you and administrator can't access the application at the same time. However, to guarantee that even in single application you can't start more than one operation affecting database, you have to block UI. Either disable buttons or put glass pane on top of UI. Modal progress dialog is also helpful.

Is it slower to use threads?

No, it is not slower if done right. Slow operation will take as long as it takes. You can't fix it with threads, but you can, either keep speed (perceived) the same while providing nice, non blocking UI or you can do more than one slow operation at a time and therefore increase that perceived speed.

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