import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.Map.Entry; import java.util.concurrent.atomic.AtomicInteger; public class Environment { Set livingCells; Map surroundingCells = new HashMap(); Environment(Set ps) { livingCells = ps; } public Set getPoints() { return livingCells; } public Environment next() { Set newPoints = new HashSet(); for (Entry s : surroundingCells.entrySet()) { if (s.getValue().intValue() == 2 || s.getValue().intValue() == 3) { if (livingCells.contains(s.getKey()) || s.getValue().intValue() == 3) { newPoints.add(s.getKey()); } } } return new Environment(newPoints); } Object _lock = new Object(); void increment(Point p) { for (Point q : p.neighbours()) { if (!surroundingCells.containsKey(q)) { synchronized(_lock) { if (!surroundingCells.containsKey(q)) { surroundingCells.put(q, new AtomicInteger(1)); continue; } } } surroundingCells.get(q).incrementAndGet(); } } }