Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1453 issue, From int to LongAdder #1531

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

package org.eclipse.collections.impl.block.procedure;

import java.util.concurrent.atomic.LongAdder;

import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.primitive.IntFunction;
import org.eclipse.collections.api.block.procedure.Procedure;
Expand All @@ -23,22 +25,24 @@ public class SumOfIntProcedure<T> implements Procedure<T>
private static final long serialVersionUID = 2L;

private final IntFunction<? super T> function;
private int result;
private final LongAdder result; // change from int to longAdder its more thread safe

public SumOfIntProcedure(IntFunction<? super T> function)
{
this.function = function;
this.result = new LongAdder(); // change
}

public int getResult()
public long getResult()
{
return this.result;
}
return this.result.sum();
} // getting the sum from LongAdder

@Override
public void value(T each)
{
this.result += this.function.intValueOf(each);
this.result.add(this.function.intValueOf(each));
}
}

// When you use it in a parallel stream, it should be less prone to overflow errors due to concurrent updates.
// we can also adjust the number of threads based on system's capabilities by creating a new variable.
Loading