【发布时间】:2013-10-28 21:34:27
【问题描述】:
我正在编写一个模拟game of life 的多线程Java 程序。每个细胞有一个线程计算细胞活力值的下一个值。我解决了 %95 的问题线程正常工作并同步,但我不知道如何在每一步之后打印出单元矩阵。我的线程中有以下运行方法:
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
}
}
我能想到的一种可能的解决方案是考虑迭代次数来分配内存。例如,如果我有 mxn 单元矩阵和 k 次迭代,我需要 mxnxk 3d 数组。然后我在这个数组中存储带有适当索引的输出,并在执行后输出。但是由于内存使用,这个解决方案似乎非常糟糕。当所有单元格一起更改时,我需要一种解决方法来打印矩阵的快照。
【问题讨论】:
-
所以你是说矩阵是共享的,属于所有线程?你想以thread-safe 的方式展示它吗?
标签: java multithreading