import java.util.LinkedList; import java.util.Queue; public class GOLThread extends Thread { public GOLThread(GameOfLife gol) { this.gol = gol; } GameOfLife gol; Queue work = new LinkedList<>(); boolean exiting = false; boolean pointsProcessed = false; public void exiting() { exiting = true; } public Environment getEnv() { return gol.env; } public void pointsProcessed() { pointsProcessed = true; } public void addPoint(Point p) { work.add(p); } @Override public void run() { while (true) { while (!work.isEmpty() && !exiting) { Point point = work.poll(); for (Point p : point.neighbours()) { getEnv().increment(p); } } if (exiting) { return; } if (pointsProcessed) { gol.threadFinished(); pointsProcessed = false; } try { sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } }