Starting a thread does not trigger a start

I am confused about a strange problem. Basically the situation is this. I implemented runnable in my class, pass the class in a new thread, override my run() method in the class that implements runnable, and then start the thread. However, my start() method never calls my run() method. I am looking for forums, but I cannot find another similar problem.

The following is sample code:

 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 } 

Any guidance would be very helpful! Thanks a ton!

+7
source share
5 answers

How do you know that this method is never called?

A simple test below works. Therefore, there is no problem with creating a thread and starting it inside the constructor. So something else happens that stops you from seeing sendEmail() called.

 public class Test implements Runnable { Thread runner; public Test() { this.runner = new Thread(this); this.runner.start(); } @Override public void run() { System.out.println("ya"); } public static void main(String[] args) { new Test(); } } 
+8
source

I think the problem is that you pass this one before the constructor call completes. This may help you: https://stackoverflow.com/a/316618/

+3
source

do not use runner = new Thread(this); in the constructor

move " runner = new Thread(this); runner.start();

for the init function, create an instance using the new one and call this init ()

+1
source
 runner = new Thread(this); runner.start(); 

Your this object will not be correctly initialized until the constructor returns. So move it somewhere, possibly to where your spawning this new stream.

+1
source

Move it from the constructor. There is no "this" inside the constructor.

-one
source

All Articles