0

I have piece of code below in which 5 different threads are created and then correspondingly run method is being called , please advise how can i add synchronization in this as i want when one thread is calling run method then no other thread should enter inside run method

public class SSendEmail implements Runnable {

   public static void main(String [] args) throws Exception, IOException, Exception{

      for(int i=0;i<5;i++) {
          new Thread(new SSendMail()).start();
      }
   }

   public void run() {

    String smtpHost = "xxx";
    String mailSmtpPort = "000";
    String mailTo[] = {"sart@wer.com" };
    String mailCc[] = {"sart@wer.com" };

    xxsendmail(mailTo, mailCc, "sendername",
            "testsubject.", "testsubject..", smtpHost , mailSmtpPort);
  }  
}
JazzCat
  • 4,243
  • 1
  • 26
  • 40
sss
  • 161
  • 1
  • 1
  • 13

3 Answers3

0

It makes no sense to spawn 5 threads that has to synchronize their run methods. Why not just run them in sequence? However, if you still want do it, one way is to pass the same instance of an Object to SSendMail and synchronize on that object in your run method. i.e. create an object before the for loop and pass it to SSendMail constructor.

Sason Ohanian
  • 795
  • 5
  • 16
0

Synchronize the run method to prevent other threads entering into the run method as follows.

public class SSendEmail implements Runnable {

public static void main(String [] args) throws Exception, IOException, Exception{

  SSendMail sendMail = new SSendMail();
  for(int i=0;i<5;i++) {
      new Thread(sendMail).start();
  }

}

public synchronized void run() {

String smtpHost = "xxx";
String mailSmtpPort = "000";
String mailTo[] = {"sart@wer.com" };
String mailCc[] = {"sart@wer.com" };

xxsendmail(mailTo, mailCc, "sendername",
        "testsubject.", "testsubject..", smtpHost , mailSmtpPort);

}
}

BTW, I wonder why do u want to prevent other threads enter into the above run method.

prasadr
  • 1
  • 3
  • This wont prevent the 5 separate instances of SSendMail to run in parallel. – Sason Ohanian Apr 09 '16 at 11:57
  • Thanks for letting me know about this, does it mean synchronized keyword won't work on run methods? – prasadr Apr 09 '16 at 12:05
  • No, it works on run methods. I just saw that you moved creating instance of SSendMail to outside the loop and create only one instance, my bad. This will work to. – Sason Ohanian Apr 09 '16 at 12:14
0

You can use either synchronized(X.class) or synchronized(this). synchronized(X.class) is used to make sure that there is exactly one Thread in the block and synchronized(this) the block is guarded by the instance. For every instance only one thread may enter the block.Also you have to use proper wait() and notify() according to your requirement.

public class SSendEmail implements Runnable {

       public static void main(String [] args) throws Exception, IOException, Exception{

          for(int i=0;i<5;i++) {
              new Thread(new SSendMail()).start();
          }
       }

       public void run() {
         synchronized(SSendEmail.class) {
          String smtpHost = "xxx";
          String mailSmtpPort = "000";
          String mailTo[] = {"sart@wer.com" };
          String mailCc[] = {"sart@wer.com" };

           xxsendmail(mailTo, mailCc, "sendername",
                "testsubject.", "testsubject..", smtpHost , mailSmtpPort);
      }  

    }

Reference 1Reference 2

wait() and notify()

Lets say if you have some condition to match before taking action on method.According to your example you are sending a emails but there are some situations that there would be empty array of emails so some of your threads would fail if array was empty.so that kind of scenario you can use wait() and notify().

wait() :Running thread will be blocked until a specific condition is met. notify() :Notify to blocked thread to run again.Also you can use notifyAll() to let know multiple threads.

Community
  • 1
  • 1
gihan-maduranga
  • 4,381
  • 5
  • 41
  • 74
  • Thanks perfect explanation request you to also please show how to add wait and notify in it also that will be great to grasp, thanks in advance – sss Apr 09 '16 at 18:06