0

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);
        }
  • whats in `initials`? Can you check that `finalCorrect.startsWith(x)` returns true? – areus Jan 12 '21 at 11:23
  • initials is a list of the initials of all employees. finalCorrect.startsWith(x) return false throught all the loop – Patryk Chojnacki Jan 12 '21 at 11:31
  • that's the error then, if the two initial chars of `finalCorrect` are not in the list `initials` you cannot found it – areus Jan 12 '21 at 11:38

1 Answers1

0

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));
areus
  • 2,880
  • 2
  • 7
  • 17
  • Thats thread kiler work very well. The loop where i checked the password didnt work because my auto Employee Generator (where i download a data from some fake name generator website and creating new Employee object) but when i create some Employe object by myself and add to list that work fine. I have no idea why Employe generator broke my if statment... – Patryk Chojnacki Jan 12 '21 at 16:34