Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Solving a Parallel Streams Puzzler
import java.util.List;
import static;
public class ParallelStreamsPuzzler {
public static void main(String[] args) {
List<Transaction> transactions
= LongStream.rangeClosed(0, 1_000)
Account myAccount = new Account();
// bug
System.out.println("The total balance is " + myAccount.getAvailableAmount());
static class Account {
private long total = 0;
public void process(Transaction transaction) {
public void add(long amount) {
total += amount;
public long getAvailableAmount(){
return total;
static class Transaction {
private final long value;
public Transaction(long value) {
this.value = value;
public long getValue() {
return value;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment