在下面的代码中,我想在invokeLater完成后做一些事情。 1)由于调度程序调用了setModel,我无法使用invokeAndWait。 2)我无法在线程内部更改最终变量的值!那么我该如何等待involeLater先完成?
boolean isDone=false;
SwingUtilities.invokeLater(new Runnable(){public void run(){
setModel(new DefaultTableModel(oRecordTable, sIP_TABLE_COL_NAMES_LIST));
isDone = true; // CAN NOT BE CHANGED HERE, AS HAS TO BE FINAL
}});
while (!idDone){
//wait
}
// do something here after the invokeLater is done
答案 0 :(得分:1)
你可以使用CountDownLatch来做到这一点。但这与使用invokeAndWait()完全相同,阻塞了当前的Thread,所以它没有任何意义。
正确的:
SwingUtilities.invokeLater(new Runnable(){public void run(){
setModel(new DefaultTableModel(oRecordTable, sIP_TABLE_COL_NAMES_LIST));
isDone = true; // CAN NOT BE CHANGED HERE, AS HAS TO BE FINAL
// do something here after the invokeLater is done <=====
}});
CountDownLatch(无用,见上文)
final CountDownLatch latch = new CountDownLatch(1);
SwingUtilities.invokeLater(new Runnable(){public void run(){
setModel(new DefaultTableModel(oRecordTable, sIP_TABLE_COL_NAMES_LIST));
isDone = true; // CAN NOT BE CHANGED HERE, AS HAS TO BE FINAL
latch.countDown();
}});
latch.await(); // could use invokeAndWait as well if blocking thread here ..