我已经引用了这个先前的问题以及其他来源,但无法让 CountDownLatch 正常工作。
背景:mainFrame 创建名为 dataEntryFrame 的新 Frame。当点击dataEntryFrame“提交”按钮时,记录添加到数据库和dataEntryFrame被处理。此时,mainFrame 应该清除并重新加载一个显示所有记录的 jList。
问题:加载 dataEntryFrame 时,java 冻结,dataEntryFrame 组件永远不会加载。我无法通过这部分......然后,在 DataEntryFrame 中,CountDownLatch 应该只在单击提交按钮后递减,成功地将记录添加到数据库表中,然后自行处理。或者当用户点击取消...
代码:来自 MainFrame
clearList();
CountDownLatch dataEntryDone = new CountDownLatch(1);
DataEntryFrame f = new DataEntryFrame(dataEntryDone);
Thread newThread = new Thread(f);
newThread.start();
dataEntryDone.await();
reLoadList();
代码:来自 DataEntryFrame
public void run(){
initComponents();
loadOtherData();
this.setVisible(true);
}
void submit(){
addRecord();
this.dispose()
dataEntryDone.countDown();
}