我對一個奇怪的問題感到困惑。基本上情況就是這樣。我在我的類中實現了runnable,我在新線程中傳遞類,我重寫了實現runnable的類中的run()
方法,然後啓動線程。但是,我的start()
方法永遠不會調用我的run()
方法。我搜索論壇,但我似乎無法找到另一個類似的問題。線程啓動不會調用運行
下面是我的示例代碼:
public class EmailManager implements Runnable {
PortalManagementSBLocal pmbr= this.lookupPortalManagementSB();
Thread runner;
String emailServerName = "";
String smtpPort = "";
String emailTo = "";
String emailFrom = "";
String mailer = "JavaMailer";
String subject = "";
String message = "";
public EmailManager() {
}//default constructor
public EmailManager(String emailTo, String subject, String message){
this.emailTo=emailTo;
this.subject = subject;
this.message = message;
//need to make this dynamic
this.emailFrom = pmbr.getEmailFrom();
this.emailServerName = pmbr.getEmailServerName();
this.smtpPort = pmbr.getEmailSMTPPort();
//runner = new Thread(this,"Email");
runner = new Thread(this);
runner.start();
System.out.println("***** Email Thread running...");
}
@Override
public void run(){
sendEmail(); //This is never called
}
將非常感謝任何指導!萬分感謝!
你確定start()被調用?在sendMail()被調用之前,你可以在run()的開始處添加日誌消息嗎? –
您是否構建了派生類型的EmailManager? –
討厭的設計,實例化線程並在構造函數中啓動它。幾乎肯定會產生競爭條件。 – Qwerky