首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >并发元胞自动机执行器移动

并发元胞自动机执行器移动
EN

Stack Overflow用户
提问于 2016-12-27 05:48:02
回答 1查看 138关注 0票数 0

我有一个二维细胞自动机。在某些单元中可能有一个参与者(代理)。每个参与者都是一个线程。我需要根据actor的单元格周围的9个单元格移动actor。如果某个演员在他的社区里有这些细胞,他必须等到第一个演员搬家。但我希望允许同时移动没有公共邻域的演员。因此,(4,5)中的参与者可以与(10,5)中的参与者同时移动,因为它们没有共同的邻域。

最好的解决方案是什么?你能提个建议吗?

EN

回答 1

Stack Overflow用户

发布于 2016-12-27 16:50:34

粗略的想法如下。

创建单元对象矩阵,该矩阵将用于synchronization

  • Assign Actor to cells
  1. 每当Actor移动到另一个单元时,它必须在cells

上获得监视器

请注意,Actor开始移动的单元格在下面的代码中不受保护。另外,如果填充的每个单元都有一个Actor,您会期望什么?

代码语言:javascript
复制
import java.util.ArrayList;
import java.util.List;

public class CellularAutomata {

    public static void main(String ... args) throws InterruptedException {
        final int rows = 5;
        final int cols = 5;
        Cell[][] matrix = new Cell[rows][cols];
        List<Actor> actors = new ArrayList<>();
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                matrix[i][j] = new Cell();
                //populate actors
                if ((i + j) % 2 == 0) {
                    Actor actor = new Actor(matrix, i, j);
                    actor.setName(String.format("Actor %d %d", i, j));
                    actors.add(actor);
                }
            }
        }
        for (Actor actor : actors) {
            actor.start();
        }
        for (Actor actor : actors) {
            actor.join();
        }
    }

    public static class Cell {}

    public static class Actor extends Thread {

        private final static int[][] circleMoves = {
                {-1, -1}, {-1, 0}, {-1, 1}
                , {0, 1}, {1, 1}, {1, 0}
                , {1, -1}, {0, -1}, {0, 0}
        };
        private final Cell[][] matrix;
        private final int row;
        private final int col;

        public Actor(Cell[][] matrix, int row, int col) {
            this.matrix = matrix;
            this.row = row;
            this.col = col;
        }

        @Override
        public void run() {
            for (int i = 0; i < circleMoves.length; i++) {
                int nextRow = row + circleMoves[i][0];
                int nextCol = col + circleMoves[i][1];
                if (nextRow >= 0 && nextRow < matrix.length
                        && nextCol >= 0 && nextCol < matrix[nextRow].length) {
                    Cell targetCell = matrix[nextRow][nextCol];
                    System.out.println(Thread.currentThread().getName() + " waiting for cell (" + nextRow + ";" + nextCol + ")");
                    synchronized (targetCell) {
                        try {
                            System.out.println(Thread.currentThread().getName() + " moved to cell (" + nextRow + ";" + nextCol + ")");
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            throw new IllegalStateException(e);
                        }
                    }
                }
            }
        }

    }

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41336590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档