用lambda和Java调用数独多线程

用lambda和Java调用数独多线程,java,multithreading,lambda,sudoku,callable,Java,Multithreading,Lambda,Sudoku,Callable,我制作了一个程序,检查完成的9x9数独矩阵是否正确。我想创建线程,同时检查行、列和区域,看看数字1-9是否出现在其中。我不熟悉使用Callable和lambda。以下是我目前的代码: public class Sudoku { public boolean testBoard(int[][] board) throws InterruptedException, ExecutionException { List<Callable<Boolean>&g

我制作了一个程序,检查完成的9x9数独矩阵是否正确。我想创建线程,同时检查行、列和区域,看看数字1-9是否出现在其中。我不熟悉使用Callable和lambda。以下是我目前的代码:

public class Sudoku {

    public boolean testBoard(int[][] board) throws InterruptedException, ExecutionException {
        List<Callable<Boolean>> tests = new ArrayList<>();
        tests.add(() -> testCols(board));
        tests.add(() -> testRegions(board));
        tests.add(() -> testRows(board));
        tests.add(() -> testSize(board));

        /*Maybe store this threadPool in a field so you dont create it everytime*/
        ExecutorService threadPool = Executors.newCachedThreadPool();
        List<Future<Boolean>> results = threadPool.invokeAll(tests);

        for (Future<Boolean> future : results) {
            if (!Boolean.TRUE.equals(future.get())) {
                return false;
            }
        }
        return true;
    }

    // check that the board is 9 x 9
    boolean testSize(int[][] board) {
        if (board.length != 9) {
            return false;
        }
        for (int i = 0; i < board.length; i++) {
            if (board[i].length != 9) {
                return false;
            } else;
        }
        return true;
    }

    // check that the digits 1-9 each appear exactly once in the given array
    boolean checkDigits(int[] array) {
        if (array.length != 9) {
            return false;
        }
        int[] counts = new int[10];
        for (int i = 0; i
                < array.length; i++) {
    // invalid number
            if (array[i] < 1 || array[i] > 9) {
                return false;
            }
    // we have already seen this number
            if (counts[array[i]] > 0) {
                return false;
            }
            counts[array[i]]++;
        }
        return true;
    }
    // return true if all rows are correct

    boolean testRows(int[][] board) {
        for (int i = 0; i < board.length; i++) {
            if (!checkDigits(board[i])) {
                return false;
            }
        }
        return true;
    }
    // return true if all columns are correct

    boolean testCols(int[][] board) {
        int[] tmp = new int[board.length];
        for (int col = 0; col < board.length; col++) {
    // fill a temp array with every element of the column
            for (int row = 0; row < board.length; row++) {
                tmp[row]
                        = board[row][col];
            }
    // check to make sure it has all the right digits
            if (!checkDigits(tmp)) {
                return false;
            }
        }
        return true;
    }
    // return true if every region is correct

    boolean testRegions(int[][] board) {
    //loop through each region, passing the indices of the upper-left corner to the next method
    //note that we increment row and column counters by 3 here
        for (int row = 0; row < board.length; row += 3) {
            for (int col = 0; col
                    < board.length; col += 3) {
                if (!testRegion(board, row, col)) {
                    return false;
                }
            }
        }
        return true;
    }
    // test a specific region, given the upper left corner

    boolean testRegion(int[][] board, int startRow, int startCol) {
        int[] tmp = new int[board.length];
    // fill a temporary array with every element of the region
        int index = 0;
        for (int row = startRow; row < startRow + 3; row++) {
            for (int col = startCol; col < startCol + 3; col++) {
                tmp[index]
                        = board[row][col];
                index++;
            }
        }
    // check if we have all of the right digits in the region
        return checkDigits(tmp);
    }
}
有人试图教我如何使用callable和lambda,但我还是有点困惑。但是,我的代码现在确实可以在NetBeans中正确运行,在打印出结果之后,它将继续运行,直到我手动终止它。我不知道为什么?我认为我的代码中必须有一个语句块,如下所示:

Callable<Boolean> callable = () -> Boolean.valueOf(testCols(board));
Callable<Boolean> callable = () -> Boolean.valueOf(testRows(board));
Callable<Boolean> callable = () -> Boolean.valueOf(testRegions(board));
Callable<Boolean> callable = () -> Boolean.valueOf(testSize(board));
Callable Callable=()->Boolean.valueOf(testCols(board));
Callable Callable=()->Boolean.valueOf(testRows(board));
Callable Callable=()->Boolean.valueOf(testRegions(board));
Callable Callable=()->Boolean.valueOf(testSize(board));

但是我不知道这些线放在哪里?我无法将它们放入构造函数中,因为“board”尚未初始化。我相信这是一个简单的解决办法,我只是卡住了,因为我是新手。有什么帮助吗?

根据我的评论,我相信您的问题是,在向executor服务添加所有可调用项并调用它们之后,您还没有对其调用
shutdown()
。我建议你这样做:

ExecutorService threadPool = Executors.newCachedThreadPool();
List<Future<Boolean>> results = threadPool.invokeAll(tests);
threadPool.shutdown(); // ******* add this! *********
ExecutorService threadPool=Executors.newCachedThreadPool();
列表结果=threadPool.invokeAll(测试);
threadPool.shutdown();//****加上这个*********

这将有助于在线程池的所有可调用项完成其操作后关闭线程池。

根据我的评论,我相信您的问题在于,在将所有可调用项添加到executor服务并全部调用它们之后,您没有对executor服务调用
shutdown()
。我建议你这样做:

ExecutorService threadPool = Executors.newCachedThreadPool();
List<Future<Boolean>> results = threadPool.invokeAll(tests);
threadPool.shutdown(); // ******* add this! *********
ExecutorService threadPool=Executors.newCachedThreadPool();
列表结果=threadPool.invokeAll(测试);
threadPool.shutdown();//****加上这个*********

这将有助于在线程池的所有可调用项完成操作后关闭线程池。

我不确定这是否与您的问题有关,但在将所有可调用项添加到ExecutorService后,我认为您应该调用
shutdown()
在ExecutorService上。我不确定这是否与您的问题有关,但在将所有可调用项添加到ExecutorService后,我认为您应该在ExecutorService上调用
shutdown()
。这就解决了问题。非常感谢你!这就解决了问题。非常感谢你!
ExecutorService threadPool = Executors.newCachedThreadPool();
List<Future<Boolean>> results = threadPool.invokeAll(tests);
threadPool.shutdown(); // ******* add this! *********