making program to send mail by different threads at the same time through parallel processing

后端 未结 2 1184
無奈伤痛
無奈伤痛 2021-01-24 03:43

I have the below program which send the mail using java mail api , now this the is the simple program i have developed now i want to modify in terms of parallel execution by usi

2条回答
  •  醉梦人生
    2021-01-24 04:17

    As long your only requirement is that 5 threads should work concurrent, you are done with something like this:

    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);
      }  
    }
    

    You will use an ExecutorService when more control is needed. E.g. ThreadPooleExecutor to limit the number of concurrent running threads when you have continues new threads, but you want limit to, say, 10 threads running at the same time.

提交回复
热议问题