Slide 74
Slide 74 text
Thread per cell vs. Thread per core
public class ThreadPerCoreGameOfLife extends GameOfLife {
private final List cellsGroups;
public ThreadPerCoreGameOfLife(Dimensions dim, boolean[][] seed, int period, Channel gridChannel,
boolean logRate, boolean useVirtualThreads, BlockingRendezVous.Type type) {
super(dim, seed, period, gridChannel, logRate, useVirtualThreads, type);
cellsGroups = createCellGroups();
}
private List createCellGroups() {
List cellsGroups = new ArrayList<>();
int nThread = Runtime.getRuntime().availableProcessors();
int cellsPerGroup = (int) Math.round( (double) cells.size() / (double) (nThread-1) );
int groupStart = 0;
for (int i = 0; i <= nThread-1; i++) {
cellsGroups.add( new CellsGroup( cells.subList(groupStart, groupStart + cellsPerGroup) ) );
groupStart += cellsPerGroup;
}
cellsGroups.add( new CellsGroup( cells.subList(groupStart, cells.size()) ) );
return cellsGroups;
}
@Override protected void startCells() { cellsGroups.forEach( cg -> runner.accept(cg::run) ); }
@Override protected int getThreadPoolSize() { return Runtime.getRuntime().availableProcessors(); }
}