• Post Reply Bookmark Topic Watch Topic
  • New Topic

Concurent access to table model in Swing  RSS feed

 
Milos Djokovic
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, everyone!

I have a custom table model that extends javax.swing.table.AbstractTableModel and it's registered as a model for JTable. Other threads modify data in this table using lock on custom table object, and notify GUI on change by calling AbstractTableModel.fireTableDataChanged().

Is this safe? Does swing lock the table model when reading data needed for repainting table? Should I use two tables, one for GUI, and one for the rest of application?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have much (read any) experience with TableModels and thread safety, however the documentation does not tell you the that AbstractTableModel is thread safe, nor did I find anywhere indicating that the table model gets locked for display. So my assumption would be no, this is not thread safe, and you should build your backing data store to be thread safe such that writes don't interfere with reads.

Should you use two different tables, one for storage, one for view? Probably not, I don't think this would help the problem much by itself. What you should do is properly choose a collection to store the data in, and then control reads/writes in a thread-safe manner.

Check out the java.util.concurrent package for some collections that could be used to store data in a relatively safe manner. There are a bunch of them so you should consider how your application gets used before deciding which is the best way to store data.
 
Mikko Kohtamäki
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can do

And Vector is synchronized already
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like everything else in Swing, you should do things that can change the GUI in the Swing thread only. If you do that, you won't have to ask the question you just asked. Have you read the Swing threading tutorial?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!