Why does JTable in Java Swing use Vector as constr

2019-01-27 06:48发布

问题:

Does anyone know why JTable in Java Swing use Vector as constructor params: like JTable(Vector rowData, Vector columnNames), although Vector is marked as obsolete collection. (I know there are other options too but using Vector is much more convenient). Does it have anything related to thread synchronization?

回答1:

While Vector is neither obsolete nor deprecated, it is a legacy of the original DefaultTableModel; later, it "was retrofitted to implement the List interface." Vector is synchronized, but using it correctly for this feature effectively relies on an implementation detail. Instead, rely on the Memory Consistency Properties of classes whose methods provide a happens-before relation. For example, SwingWorker, which implements Future<T> and RunnableFuture<T>, is particularly convenient for reliably updating a table's model on the event dispatch thread. This obviates the need to synchronize the model's internal data structure(s) redundantly.