Java 螺纹康威&x27;“生命的游戏给了我们”;lambda表达式中使用的变量应为final或有效final“;

Java 螺纹康威&x27;“生命的游戏给了我们”;lambda表达式中使用的变量应为final或有效final“;,java,lambda,Java,Lambda,我正在尝试更新一点我的Java,因为我经常使用遗留代码,而且那里甚至没有Java8(这意味着lambda表达式不是它的一部分)。所以我有一个项目要做一些线程,现在我已经筋疲力尽了,无法很好地理解我的错误。目前,我有一些预期的错误,但我很确定它们正在发生,因为我的{}放错了地方。所以我在这里谦恭地请求一些帮助来找出我现在到底做错了什么。谢谢你 import java.util.ArrayList; public class Life implements Runnable { stat

我正在尝试更新一点我的Java,因为我经常使用遗留代码,而且那里甚至没有Java8(这意味着lambda表达式不是它的一部分)。所以我有一个项目要做一些线程,现在我已经筋疲力尽了,无法很好地理解我的错误。目前,我有一些
预期的
错误,但我很确定它们正在发生,因为我的
{}
放错了地方。所以我在这里谦恭地请求一些帮助来找出我现在到底做错了什么。谢谢你

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.");

}
import java.util.ArrayList;
公共类生命实现Runnable{
静态双秒=0.001;
静态长初始值=System.currentTimeMillis();
final int l;//lambda表达式需要final
公开募捐{
//需要遵守Runnable接口
}
//一个简单的Java程序来实现生活游戏
//发件人:https://www.geeksforgeeks.org/program-for-conways-game-of-life/
公共静态void main(字符串[]args){
int M=10,N=10;
//初始化网格。
int[][]网格={
{ 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 }
};
//显示网格
System.out.println(“原始生成”);
for(int i=0;i{
for(int m=1;m对于(inti=-1;i来说,缩进肯定到处都是。许多编辑器会为您解决这个问题

您的两个
for
语句没有关联的大括号,看起来似乎应该存在。我强烈建议始终将大括号与
for
if
while
一起使用


提供给
Thread
构造函数的
Runnable
lambda正在使用外部作用域中的
l
。您可能希望在
Thread
构造时得到
l
的值,而不是在执行时设置的最后一个值。最简单的修复方法是立即复制到
for
循环。

可能就是它(构造的局部变量)。非常感谢!这个问题的答案(由Tom H给出,如下)表明这不是一个关于线程的问题。我正在删除多线程标记,并替换lambda标记。感谢清理,@SolomonSlow。