使程序通过并行处理同时通过不同的线程发送邮件 [英] making program to send mail by different threads at the same time through parallel processing

查看:87
本文介绍了使程序通过并行处理同时通过不同的线程发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下程序使用java邮件api发送邮件,现在这是我现在开发的简单程序我想通过使用executorframework来修改并行执行,我想要5个不同的线程独立应该触发我的这个程序但是那5个不同的线程应该同时触发

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 using executorframework that i want that 5 different threads independently should trigger my this program but those 5 different threads should trigger simultaneously at the same time

让我们说有五个不同的线程t1,t2,t3,t4和t5然后所有这些线程应该独立点击我的函数,这是main(@)现在正在调用rite但同时

lets say there are five different threads t1,t2,t3,t4 and t5 then all of them should independently hit my function which is main(@) is calling rite now but at the same time

下面是我的java代码

below is my java code

public class SSendEmail {

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

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






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


    }  


推荐答案

只要你的唯一要求是5个线程应该并发工作,你就完成了这样的事情:

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

当需要更多控制时,您将使用ExecutorService。例如。 ThreadPooleExecutor 在你有新的线程继续时限制并发运行线程的数量,但你想要限制,比如10个线程同时运行。

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.

这篇关于使程序通过并行处理同时通过不同的线程发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆