Threading Conway的《人生游戏》给出了“ lambda表达式中使用的变量应该是最终的,或者实际上是最终的”

时间:2019-12-12 03:20:53

标签: java lambda

我正在尝试更新Java的一部分,因为我处理了很多遗留代码,而我们那里甚至没有Java 8(这意味着lambda表达式不是其中的一部分)。所以我有一个项目要做一些线程处理,现在我精疲力尽,无法再完全理解我的错误了。目前,我有一些<identifier> expected错误,但是我可以肯定它们正在发生,因为我的{}放错了地方。所以我在这里谦虚地寻求帮助,以弄清楚我现在到底在做什么错。请,谢谢。

import java.util.ArrayList;

public class Life implements Runnable {

    static double second = 0.001;
    static long initial = System.currentTimeMillis();
    final int l; // Final is required for lambda expressions

    public void run() {
        // Needed to comply with the Runnable interface
    }

    // A simple Java program to implement Game of Life
    // From: https://www.geeksforgeeks.org/program-for-conways-game-of-life/
    public static void main(String[] args) {

        int M = 10, N = 10;

        // Intiliazing the grid.
        int[][] grid = {
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
            { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 },
            { 0, 0, 0, 0, 1, 0, 1, 0, 0, 0 },
            { 0, 0, 0, 0, 0, 0, 1, 1, 0, 1 },
            { 1, 1, 0, 0, 0, 0, 0, 1, 0, 1 },
            { 0, 0, 0, 1, 1, 0, 0, 0, 1, 0 },
            { 0, 0, 1, 1, 0, 0, 0, 0, 1, 1 },
            { 0, 0, 0, 0, 0, 1, 0, 0, 0, 1 },
            { 0, 1, 1, 1, 1, 0, 0, 0, 1, 1 },
            { 0, 0, 0, 1, 0, 0, 0, 0, 1, 1 }
        };

        // Displaying the grid
        System.out.println("Original Generation");
        for (int i = 0; i < M; i++)
        {
            for (int j = 0; j < N; j++)
            {
                if (grid[i][j] == 0)
                    System.out.print(".");
                else
                    System.out.print("*");
            }
            System.out.println();
        }
        System.out.println();
        nextGeneration(grid, M, N);
    }

    // Function to print next generation
    static void nextGeneration(int grid[][], int M, int N) {
        ArrayList<Thread> threads = new ArrayList<>();
        int[][] future = new int[M][N];

        // Loop through every cell
        for (int l = 1; l < M - 1; l++) {
            Thread t = new Thread (() -> {
                for (int m = 1; m < N - 1; m++) {

                // finding no Of Neighbours that are alive
                int aliveNeighbours = 0;
                for (int i = -1; i <= 1; i++)
                    for (int j = -1; j <= 1; j++)
                        aliveNeighbours += grid[l + i][m + j];
                        System.out.println( "Alive Neighbours Runtime: " + (System.currentTimeMillis() - initial) / second + " seconds.");
                // The cell needs to be subtracted from
                // its neighbours as it was counted before
                aliveNeighbours -= grid[l][m];

                // Implementing the Rules of Life

                    // Cell is lonely and dies
                    if ((grid[l][m] == 1) && (aliveNeighbours < 2))
                        future[l][m] = 0;

                    // Cell dies due to over population
                    else if ((grid[l][m] == 1) && (aliveNeighbours > 3))
                        future[l][m] = 0;

                    // A new cell is born
                    else if ((grid[l][m] == 0) && (aliveNeighbours == 3))
                        future[l][m] = 1;

                    // Remains the same
                    else
                        future[l][m] = grid[l][m];
                    }
                });

                t.start();
                threads.add(t);
            }

            for(Thread t : threads)
                t.join();
        }

        System.out.println("Next Generation");

        for (int i = 0; i < M; i++) {
            for (int j = 0; j < N; j++) {
                if (future[i][j] == 0)
                    System.out.print(".");
                else
                    System.out.print("*");
            }
            System.out.println();
        }
    System.out.println( "Total runtime was: " + (System.currentTimeMillis() - initial) / second + " seconds.");

}

1 个答案:

答案 0 :(得分:3)

缩进肯定到处都是。许多编辑器会为您修复该问题。

您的几个for语句没有关联的括号,看起来好像应该存在。我强烈建议始终将括号与forifwhile等一起使用。

提供给Runnable构造函数的Thread lambda在外部作用域中使用l。大概您希望在构造l时使用Thread的值,而不是在执行时设置的最后一个值。最简单的解决方法是直接在for循环内复制到本地值。