人生ゲームをシミュレートするマルチスレッド Java プログラムを作成しています。セルの活性値の次の値を計算するセルごとに 1 つのスレッドがあります。問題のあるスレッドの 95 % を解決し、正しく同期しましたが、各ステップの後でセル マトリックスを出力する方法がわかりません。スレッドに次の run メソッドがあります。
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
}
}
考えられる解決策の 1 つは、反復回数を考慮してメモリを割り当てることです。たとえば、mxn セル マトリックスと k 回の反復がある場合、mxnxk 3D 配列が必要です。次に、この配列に適切なインデックスを付けて出力を保存し、実行後に出力します。しかし、この解決策は、メモリ使用量が原因で非常に悪いようです。すべてのセルが一緒に変更されたときに、マトリックスのスナップショットを印刷するための回避策が必要です。