I am writing a multithread Java program that simulates game of life. There is one thread for each cell computing the next value for cell aliveness value. I solved %95 of the problem threads working correctly and synchronized but I can't figure out how to print out cell matrix after each step. I have the following run method in my threads:
public void run(){
while(true){ //this may also be finite
calculateNeighbourCount(); //threads count their alive neighbours
calculateBarrier(); //Threads wait until all threads complete calculation before changing content
next();//threads make cell's value the next computed value.
nextBarrier(); //Threads wait until all threads complete next()
//Here I want to print out cell matrix
}
}
One possible solution I can think of is to allocate memory considering number of iterations.For example if i have mxn cell matrix and k number of iterations I need mxnxk 3d array. Then I store outputs with proper indexes in this array and output after execution.But this solution seems very bad due to the memory usage. I need a workaround to print the snapshot of the matrix when all cells changed together.