这是加入fork的正确代码吗?

时间:2014-02-07 15:12:30

标签: java parallel-processing fork-join

我开始阅读for in joinJava的内容,因此我的目标是验证JavaMail中有关联的消息。

我怀疑它是否严谨。因为我在每个spliting的两个部分中看到了一些教程lopp。我把我的所有课程代码放在下面,以展示我的所作所为。

package service.forkinjoin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.RecursiveTask;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.internet.MimeBodyPart;

import service.FileUtil;

public class SepareteMessagesProcess extends RecursiveTask<List<Message>> {

    private static final long serialVersionUID = 7126215365819834781L;
    private List<Message> listMessages;
    private static List<Message> listMessagesToDelete = new ArrayList<>();
    private final int threadCount = Runtime.getRuntime().availableProcessors();

    public SepareteMessagesProcess(List<Message> listMessages) {
        this.listMessages = listMessages;
    }

    @Override
    protected List<Message> compute() {

        if (this.listMessages.size() <= threadCount) {
            try {
                this.separateMessages(this.listMessages);
            } catch (MessagingException | IOException e) {
                e.printStackTrace();
            }
        } else {
            int[] arrayMaxIndex = this.generateIndexs(this.listMessages);
            List<SepareteMessagesProcess> list = this.splitList(this.listMessages, arrayMaxIndex);
            invokeAll(list);
        }

        return listMessagesToDelete;
    }

    private void separateMessages(List<Message> listMessages) throws MessagingException, IOException {

        for (Iterator<Message> iterator = listMessages.iterator(); iterator.hasNext();) {

            Message message = (Message) iterator.next();

            if ((this.hasNoAttachment(message.getContentType()) || (this.hasNoXmlAttachment(message)))) {
                listMessagesToDelete.add(message);
            } 
        }
    }

    private List<SepareteMessagesProcess> splitList(List<Message> listMessages, int[] arrayMaxIndex) {

        List<SepareteMessagesProcess> list = new ArrayList<>(this.threadCount);
        int end = 0;
        int start = 0;

        for (int i = 0; i < this.threadCount; i++) {
            end += (arrayMaxIndex[i]);
            list.add(new SepareteMessagesProcess(listMessages.subList(start, end)));
            start += arrayMaxIndex[i];
        }

        return list;
    }

    private int[] generateIndexs(List<Message> listMessages) {

        int value = listMessages.size() / this.threadCount;
        int[] arrayMaxIndex = new int[this.threadCount];

        for (int i = 0; i < this.threadCount; i++) {
            arrayMaxIndex[i] = value;
        }
        arrayMaxIndex[this.threadCount - 1] += listMessages.size() % threadCount;

        return arrayMaxIndex;
    }

    private boolean hasNoAttachment(String content) {
        return !content.contains("multipart/MIXED");
    }

    private boolean hasNoXmlAttachment(Message message) throws IOException, MessagingException {

        Multipart multipart = (Multipart) message.getContent();         

        for (int i = 0; i < multipart.getCount(); i++) {

            MimeBodyPart mimeBodyPart = (MimeBodyPart) multipart.getBodyPart(i);

            if (Part.ATTACHMENT.equalsIgnoreCase(mimeBodyPart.getDisposition())) {

                if  (FileUtil.isXmlFile(mimeBodyPart.getFileName())) {
                    return false;
                }
            }
        }

        return true;
    }
}

1 个答案:

答案 0 :(得分:1)

没有。而不是在这里重写教程,只需按照它给出的示例,例如:

 if (problem is small)
     directly solve problem
   else {
    split problem into independent parts
    fork new subtasks to solve each part
    join all subtasks
    compose result from subresults
}