如何实现 forkjoin 目前在 for 循环中完成的一系列操作

发布于 2024-12-11 23:25:29 字数 182 浏览 2 评论 0原文

我有一个发件人列表,我必须并行地单独发送邮件。目前,我正在迭代列表构建正文(因为它对于不同的人来说是不同的),然后发送它们。我该如何使用 forkjoin 来实现这一点。我尝试使用 recusiveAction 但我猜它仅适用于递归任务。

互联网上的所有示例都是使用 RecursiveAction 实现的。有没有其他类可以实现这个。

I have a list of senders for them I have to parallely send mails individually.Currently I am iterating over the list construct the body (as it is different for different people) and then sending them. How can I use forkjoin for this. I tried using recusiveAction but I guess its only for recursive tasks.

All the examples available in internet are implemented with RecursiveAction. Is there any other class with which I can implement this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初与友歌 2024-12-18 23:25:29

ServiceExecutors 可以很好地解决这个问题。它们带有 Java。

import java.util.*;
import java.util.concurrent.*;

public class SendMailExample
{
  public static void main(String[] args) throws Exception
  {
    ExecutorService executor = Executors.newFixedThreadPool(3);

    Collection<Future> futures = new ArrayList<Future>();
    futures.add(executor.submit(new Mailer("thread1")));
    futures.add(executor.submit(new Mailer("thread2")));
    futures.add(executor.submit(new Mailer("thread3")));

    for (Future future : futures)
    {
      future.get();
    }
    executor.shutdown();
  }

  static class Mailer implements Runnable
  {
    private Object message;

    public Mailer(Object message)
    {
      this.message = message;
    }

    public void run()
    {
      System.out.println("Sending message " + String.valueOf(message));
    }
  }
}

ServiceExecutors work nicely for this. They come with Java.

import java.util.*;
import java.util.concurrent.*;

public class SendMailExample
{
  public static void main(String[] args) throws Exception
  {
    ExecutorService executor = Executors.newFixedThreadPool(3);

    Collection<Future> futures = new ArrayList<Future>();
    futures.add(executor.submit(new Mailer("thread1")));
    futures.add(executor.submit(new Mailer("thread2")));
    futures.add(executor.submit(new Mailer("thread3")));

    for (Future future : futures)
    {
      future.get();
    }
    executor.shutdown();
  }

  static class Mailer implements Runnable
  {
    private Object message;

    public Mailer(Object message)
    {
      this.message = message;
    }

    public void run()
    {
      System.out.println("Sending message " + String.valueOf(message));
    }
  }
}
撩动你心 2024-12-18 23:25:29

我浏览后得到了一个更好的答案:

package Test1;

import java.util.*;
import java.util.concurrent.*;
import static java.util.Arrays.asList;

public class Sums
{
    static class Sum implements Callable<Long>
    {
        private final long from;
        private final long to;
        Sum(long from, long to)
        {
            this.from = from;
            this.to = to;
        }

        @Override
        public Long call()
        {
            long acc = 0;
            if(from == 0)
            {
                try
                {
                    Thread.sleep(5000);
                }
                catch (InterruptedException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            System.out.println(from);
            for (long i = from; i <= to; i++)
            {
                acc = acc + i;
            }
            return acc;
        }                
    }

    public static void main(String[] args) throws Exception
    {
        ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
        List <Future<Long>> results = executor.invokeAll(asList(
        new Sum(0, 10), new Sum(100, 1000), new Sum(10000, 1000000)
        ));
        executor.shutdown();

        for (Future<Long> result : results)
        {
            System.out.println(result.get());
        }
    }    
}

使用此代码,您将能够获得响应以及引发的任何异常。

I browsed I got a better answer:

package Test1;

import java.util.*;
import java.util.concurrent.*;
import static java.util.Arrays.asList;

public class Sums
{
    static class Sum implements Callable<Long>
    {
        private final long from;
        private final long to;
        Sum(long from, long to)
        {
            this.from = from;
            this.to = to;
        }

        @Override
        public Long call()
        {
            long acc = 0;
            if(from == 0)
            {
                try
                {
                    Thread.sleep(5000);
                }
                catch (InterruptedException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            System.out.println(from);
            for (long i = from; i <= to; i++)
            {
                acc = acc + i;
            }
            return acc;
        }                
    }

    public static void main(String[] args) throws Exception
    {
        ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
        List <Future<Long>> results = executor.invokeAll(asList(
        new Sum(0, 10), new Sum(100, 1000), new Sum(10000, 1000000)
        ));
        executor.shutdown();

        for (Future<Long> result : results)
        {
            System.out.println(result.get());
        }
    }    
}

With this code, you will be able to get the response and also any exceptions that are thrown.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文