I have created an application which queries a MySQL database for a list of items. The resulting data set is parsed into individual Objects and they are passed throughout the program in ArrayLists.
Currently, during the initial database connection, and any subsequent queries, the GUI of my program will hang. To counter this, i would like to transition my DatabaseManager class to run in a separate thread.
I am curious how I should handle passing the data between the GUI and the DatabaseManager thread.
Actually, I am more curious how i would create a single class to handle all of the database functionality that I have already implemented, but run it all in a separate thread.
The current method definitions include, but are not limited to
- initConnection()
- query(String SQLQuery)
- printResultSet()
Ideally, these functions would be called from the GUI and would return very quickly. How would i go about setting this up?
When I was in the same situation, I used an Akka typed actor for the DatabaseManager (it’s a thread underneath). I sent asynchronous requests from my table model to my “Database actor”, and then updated the GUI/AbstractTableModel with
SwingUtilities.invokeLater()from the Actor. It worked well, but you can also do it with threads manually.