N皇后-多线程版

该博客探讨了Java中解决八皇后问题的两种方法:多线程版本和非多线程版本。通过创建线程池并提交Callable任务,多线程版本实现了并发计算,而单线程版本则顺序执行。博客详细展示了代码实现,并给出了运行时间对比,以展示多线程在解决此类问题时的效率提升。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class Multithread {
    private int QUEEN_NUM = 0;
    private int[][] Checkerboard;
    private Long COUNT = 0L;

    public Multithread(int QUEEN_NUM) {
        this.QUEEN_NUM = QUEEN_NUM;
        this.Checkerboard = new int[QUEEN_NUM][QUEEN_NUM];
        this.COUNT = 0L;
    }

    public boolean check(int row, int col) {
        for (int i = row - 1; i >= 0; i--) {
            if (Checkerboard[i][col] == 1)
                return false;
        }
        for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
            if (Checkerboard[i][j] == 1)
                return false;
        }
        for (int i = row - 1, j = col + 1; i >= 0 && j < QUEEN_NUM; i--, j++) {
            if (Checkerboard[i][j] == 1)
                return false;
        }
        return true;
    }

    public Long calculate(int row,int i){
        Checkerboard[row][i] = 1;
        if (row == QUEEN_NUM - 1) {
            COUNT++;
        } else {
            calculate(row + 1);
        }
        Checkerboard[row][i] = 0;
        return COUNT;
    }

    public Long calculate(int row) {
        for (int i = 0; i < QUEEN_NUM; i++) {
            if (check(row, i)) {
                Checkerboard[row][i] = 1;
                if (row == QUEEN_NUM - 1) {
                    COUNT++;
                } else {
                    calculate(row + 1);
                }
                Checkerboard[row][i] = 0;
            }
        }
//        System.out.println("子线程结果:"+COUNT);
        return COUNT;
    }
}
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

import java.util.ArrayList;
import java.util.concurrent.*;

public class Main {
    public static int row = 16;
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ArrayList<Future<Long> > tasks = new ArrayList<>();
        ExecutorService exec = Executors.newCachedThreadPool();
        System.out.println("---------------------------多线程版本---------------------------");
        long start = System.currentTimeMillis();
        System.out.println("开始时间:"+start);
        Long sum = 0L;
        for(int i = 0;i<row;i++){

            Calculate task = new Calculate(row,i);
            Future<Long> future = exec.submit(task);
            tasks.add(future);
//            Calculate calculate = new Calculate(row,i);
//            FutureTask<Long> task1 = new FutureTask<Long>(calculate);
//            tasks.add(task1);
//            new Thread(task1).start();
        }

        System.out.println("队列长度:"+tasks.size());
        for(int i = 0;i<tasks.size();i++){
            Future<Long> temp = tasks.get(i);
            while(temp != null && !temp.isDone()){
//                System.out.println("子线程"+i+"继续监听");
                TimeUnit.MILLISECONDS.sleep(200);
            }
            System.out.println("子线程"+i+"已经运行结束");
            sum+=temp.get();
        }

        long now = System.currentTimeMillis();
        System.out.println("结束时间:"+now);
        System.out.println("使用时间:  "+(now-start)+"  ms");
        System.out.println("结果是:" + sum);
        System.out.println();
        exec.shutdown();

        System.out.println("---------------------------非多线程版本---------------------------");
        start = System.currentTimeMillis();
        System.out.println("开始时间:"+start);

        Multithread multithread =  new Multithread(row);
        sum = multithread.calculate(0);
        now = System.currentTimeMillis();
        System.out.println("结束时间:"+now);
        System.out.println("使用时间:  "+(now-start)+"  ms");
        System.out.println("结果是:" + sum);
        System.out.println();
    }
}

class Calculate implements Callable<Long>{
    private int i = 0;
    private int row = 0;

    public Calculate(int row,int i) {
        this.row = row;
        this.i = i;
    }

    @Override
    public Long call() throws Exception{
        Multithread temp = new Multithread(this.row);
        return temp.calculate(0,this.i);
    }
}

执行结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值