有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

比较两个字符串的java多线程循环

我有练习学习多线程。我有一个正确的密码,比如:xxyymdd xx是我的“员工”名单上的首字母缩写,YY是出生年份,mm是出生月份,DD是出生日期。例如:Nichole Hunter 09/10/93密码是:NH930910。 但是这个循环只知道一个密码,从首字母开始,然后是6个数字。(稍后,我必须使用另一种方法创建其他循环,并比较使用多线程的方法查找密码的速度更快,但现在这并不重要)

我有一个在多线程中工作的循环,对于每个初始值,添加一个从000000到999999的数字字符串,并检查它是否等于正确的密码。 我不知道为什么“如果”的指令永远不会被调用。有人知道如何帮助吗? 如果找到正确的密码,如何停止其他线程

executorService= Executors.newFixedThreadPool(30);
        String finalCorrect = correct;

        for(String x : inicials){
            executorService.submit(()->{
            for(int count=0;count<999999; count++){
                
                if(finalCorrect.equals(x+String.format("%06d", count).trim())){
                    System.out.println("Correct password is: "+x+String.format("%06d", count).trim());
                    System.out.println("The really password is: "+ finalCorrect);
                    break;
                }
            }
                    }
            );

        }
        executorService.shutdown();
        try {
            executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
        } catch (InterruptedException x) {
            System.out.println(x);
        }

共 (1) 个答案

  1. # 1 楼答案

    代码基本上是正确的。问题似乎出在初始化参数上。例如,如果正确的密码(finalCorrect)以NH开头,那么在列表initials中必须有值NH

    至于在找到解决方案后停止工作线程,可以调用executorService.shutdownNow()尝试停止线程。在循环内部,您必须检查中断状态

    下面是对代码的一个修改,其中添加了更多日志记录,从而停止了工作线程:

        ExecutorService executorService= Executors.newFixedThreadPool(30);
    
        for(String x : inicials){
            System.out.println("Checking " + x);
            executorService.submit(()->{
                        for(int count=0;count<999999; count++){
    
                            if(finalCorrect.equals(x+String.format("%06d", count).trim())){
                                System.out.println("Correct password is: "+x+String.format("%06d", count).trim());
                                System.out.println("The really password is: "+ finalCorrect);
                                // Attempt to stop all the threads...
                                executorService.shutdownNow();
                                break;
                            }
    
                            // Test if the thread has been requested to stop
                            if (Thread.currentThread().isInterrupted()) {
                                System.out.println("Interrupted " + x);
                                return;
                            }
                        }
                        System.out.println("End " + x);
                    }
            );
    
        }
        executorService.shutdown();
        try {
            executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
        } catch (InterruptedException x) {
            System.out.println(x);
        }
    

    使用Java 8的新Streams功能,您可以以更紧凑的形式实现相同的方法:

        inicials.parallelStream()
                .flatMap(x -> IntStream.rangeClosed(0, 999999).mapToObj(i -> x + String.format("%06d", i)))
                .filter(finalCorrect::equals)
                .findAny()
                .ifPresent(x -> System.out.println("Found correct password: " + x));