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.