Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I´m with trouble, because I want to make faster my code using threads, fork in join or any kind of parallel.

I did a code using fork in join but I saw my arraylist isn´t getting the correct values.

I don´t know if is my implementation. But I want to take off all messages without attachments, later separate by sender.

One example of my class using fork in join, but I´m looking for any suggestions.

package service.forkinjoin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.RecursiveAction;

import javax.mail.Message;
import javax.mail.MessagingException;

import service.EmailUtil;

public class ForkSortMessagesBySender extends RecursiveAction {

    private static final long serialVersionUID = -1092415796824205832L;
    private List<Message> listMessagesWithAttachment;
    private List<Message> listMessages;
    private String sender;

    public ForkSortMessagesBySender(List<Message> listMessagesWithAttachment, List<Message> listMessages, String sender) {
        this.listMessagesWithAttachment = listMessagesWithAttachment;
        this.listMessages = listMessages;
        this.sender = sender;
    }

    @Override
    protected void compute() {

        List<RecursiveAction> actions = new ArrayList<>();

        if (this.listMessagesWithAttachment.size() <= Runtime.getRuntime().availableProcessors()) {
            try {
                this.separateMessages();
            } catch (MessagingException | IOException e) {
                e.printStackTrace();
            }
        } else {

            int end = this.listMessagesWithAttachment.size() / 2;
            actions.add(new ForkSortMessagesBySender(this.listMessagesWithAttachment.subList(0, end), this.listMessages, this.sender));
            end += this.listMessages.size() % 2 == 0 ? 0 : 1;
            actions.add(new ForkSortMessagesBySender(this.listMessagesWithAttachment.subList(end, this.listMessagesWithAttachment.size()), this.listMessages, this.sender));
            invokeAll(actions);
        }
    }

    private void separateMessages() throws MessagingException, IOException {

        for (Message message : this.listMessagesWithAttachment) {
            if (EmailUtil.getSender(message).equals(this.sender)) {
                this.listMessages.add(message);
            }
        }
    }
}
share|improve this question
1  
You would probably get more answer on stackoverflow or codereview.stackexchange.com –  assylias Feb 8 at 22:39
1  
@assylias StackOverflow would be the place to go. CodeReview.se accepts only working code as far as I know. –  metacubed May 25 at 3:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.