3

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.

Jumbala
  • 4,764
  • 9
  • 45
  • 65
  • 1
    2) Disable the button while the worker is running, and enable the button once the worker is finished. – Sam Barnum Jul 12 '11 at 02:19
  • @Sam Barnum Great idea, I hadn't thought of that. – Jumbala Jul 12 '11 at 02:24
  • Rather than long explanations about how you coded your stuff, what not just psoting the relevant snippet here? That would be much better if you want to get useful feedback. – jfpoilpret Jul 12 '11 at 11:54
  • @jfpoilpret I didn't post a code snippet because I don't feel like it was relevant since I'm asking for help in general. There are other cases than the example I described that use threads and I just wanted to see if I was on the righ track before creating all the threads I need. – Jumbala Jul 12 '11 at 15:32
  • @Adam I asked because your point 3 is probably related to something bad in your code, and it is hard to say without seeing your `SwingWorker` code. – jfpoilpret Jul 12 '11 at 16:46
  • @jfpoilpret I see... I'll try to make a simplified example when I get back home if I can't figure out why it is slower... I have an idea why, though. Thanks! – Jumbala Jul 12 '11 at 17:43

2 Answers2

3

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.

Alex Gitelman
  • 24,429
  • 7
  • 52
  • 49
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.

Community
  • 1
  • 1
emboss
  • 38,880
  • 7
  • 101
  • 108
  • Thank you for the explanations, I'll take a look at my code and see how I can improve it. Right now, I'm only testing it locally, so the multiple connections is not a problem since it is in test mode in a "kiosk-like" environment. Eventually I'm going to have multiple kiosks set up that are going to use the same database. I don't know what DataSource is, though, but I'll look into it. I'm using SQLite but might change to MySQL eventually if I end up setting the database up on my website, so I don't know if that's relevant right now since I don't know what it is. – Jumbala Jul 12 '11 at 02:51
  • [DataSource](http://download.oracle.com/javase/6/docs/api/javax/sql/DataSource.html), you use them to get a connection to the database using JDBC. – emboss Jul 12 '11 at 02:53
  • Oh, seems like it's the same methods I'm using, I just didn't know it was called that... I use Connection conn = DriverManager.getConnection("jdbc:sqlite:database.sqlite") Thanks a ton for the input, I'm sure it'll improve my program by a lot. – Jumbala Jul 12 '11 at 03:05