在DbInterface類中,函數openDB()打開與服務器上Oracle數據庫的連接。出於安全原因,用戶必須在程序繼續進行連接之前在JFrame文本區輸入密碼。現在這個Jframe有一個動作偵聽器,它等待用戶輸入密碼並調用OpenDBContinue()方法。Java:如何使用線程等待來自用戶的IO,然後繼續
現在的問題是:openDB()不等待Jframe IO完成並假設數據庫已打開,將控制權返回給調用類(調用openDB()的任何人),然後繼續並開始查詢數據庫顯然失敗!
現在我該如何讓openframe()在Jframe IO上等待完成?這是給你一個想法的代碼。
public void openDB(int inFileInx,String inRemoteDBURLFull) throws FileNotFoundException
{
if(this.password!=null)
try
{ openDBcontinue(inFileInx,inRemoteDBURLFull);
}
catch(Exception exp)
{ DpmLogger.dtlException("SPDBInterfaceException:OpenDB", exp);
}
else {
passwd = new JFrame();
passwd.setLocation(SpdMain.bTabbedPanel.getWidth()/2,SpdMain.bTabbedPanel.getHeight()/2);
passwd.setTitle("Enter Passwd for user "+username);
JPasswordField p =new JPasswordField(10);
p.addActionListener(this);
p.setActionCommand(inFileInx+","+inRemoteDBURLFull);
passwd.add(p);
passwd.setPreferredSize(new Dimension(300,50));
passwd.pack();
passwd.setVisible(true);
pass=new Thread(new Runnable()
{
public void run() {
DpmLogger.dtlTraceOut("The password thread has completed and has got password from the user",DpmLogger.TRACE_RARE,myId);
}
});
try {
pass.join();
} catch (InterruptedException e)
{
DpmLogger.dtlTraceOut("Password thread unable to join",DpmLogger.TRACE_RARE,myId);
}
DpmLogger.dtlTraceOut("Password thread now joined",DpmLogger.TRACE_RARE,myId);
}
}
public void actionPerformed(ActionEvent e)
{ JTextField p=(JTextField)e.getSource();
if(password==null)
password=p.getText();
passwd.setVisible(false);
String[] inVars=e.getActionCommand().split(",");
try
{ openDBcontinue(Integer.parseInt(inVars[0]),inVars[1]);
pass.start();
}
catch(Exception exp)
{ DpmLogger.dtlException("SPDBInterfaceException:OpenDB", exp);
}
}
正如你所看到的,我試圖讓方法在join()的'pass'線程上等待。動作偵聽器在IO完成時啓動傳遞線程。但它不起作用。 OpenDB()無需等待'pass'即可運行。這是因爲該方法不在線程內?我是否必須使此DBInterface類擴展Thread類?我很困惑!
退房How to Make Dialogs什麼是錯了['JDialog'](http://docs.oracle.com/javase/tutorial/uiswing/components/dialog html的)? – MadProgrammer
所有事件都在一個單獨的線程(事件調度程序線程)中進行處理。我想你正在應用'pass.join()'在錯誤的線程中(這使**當前**線程等待通過完成),而你想在JFrame IO上等待完成。如果有任何耗時的處理,你應該考慮'SwingWorker',例如,以便你的UI不會凍結 –
哦,對不起,我使用JPasswordField(你可以在代碼中看到它)。我一定忘記提及,但那不是我的問題 – Sid