import java.util.LinkedList; import java.util.List; import java.util.concurrent.CountDownLatch; public class GameOfLife { public GameOfLife(Environment env) { this.env = env; } Environment env; List threads = new LinkedList(); public void start() { setThreadCount(1); } synchronized void setThreadCount(int numberOfThreads) { for (GOLThread golThread : threads) { golThread.exiting(); } for (GOLThread golThread : threads) { try { golThread.join(); } catch (InterruptedException e) { e.printStackTrace(); } } for (int i = 0; i < numberOfThreads; ++i) { GOLThread golThread = new GOLThread(this); threads.add(golThread); golThread.start(); } } CountDownLatch countDown; synchronized void step() { int i = 0; countDown = new CountDownLatch(threads.size()); for (Point cell : env.living) { threads.get(i % threads.size()).addPoint(cell); ++i; } for (GOLThread golThread : threads) { golThread.pointsProcessed(); } try { countDown.await(); } catch (InterruptedException e) { e.printStackTrace(); } env = env.next(); } public void threadFinished() { countDown.countDown(); } }