Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
131 views
in Technique[技术] by (71.8m points)

java - Multithread loop which comparing two Strings

I have exercise to learn multithread. I have one correct password like: xxYYmmDD xx is a initials from my "Employees" list, YY is a year of birth, mm is a month of birth and DD is a day of birth. For example: Nichole Hunter 09/10/93 password is: NH930910. But this loop know only about it a password start from initials and after that have 6 numbers. (later i have to create other loops with another method and compare which method with multithread is faster to find password but this is not important now)

I have loop which work in multithreads and for every single initial adding a String of number from 000000 to 999999 and checking it equals to correct password. I have no idea why instruction from "if" is never call. Some one know how to help? And how to stop other Threads if i find correct password?

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);
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The code is essentially correct. The problem seems to be in the initialization parameters. If, as for your example, the correct password (finalCorrect), starts with NH then inside list initials there must be the value NH.

As for stopping the working threads once the solution is found, you can call executorService.shutdownNow() to attempt to stop the threads. Inside the loop you will have to check for the interrupted status.

Here is an adaptation of your code with a bit more logging, that stops the working threads:

    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);
    }

Using the new Streams features of Java 8, you could implement the same aPproach with a more compact form:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...