Davinho
Davinho

Reputation: 107

Java String permutation ExecutorService

I would like to print all String permutations with ExecutorService but it is not faster compare to run it on single thread.

My Code:

  private static void setUpThreads(String startCharacter, char inputLength[], String withoutStartChar, int threadnumber) {
    ExecutorService exec = Executors.newFixedThreadPool(threadnumber);
    exec.execute(() -> permutation(startCharacter, inputLength, 0, withoutStartChar));
    exec.shutdown();
    try {
        exec.awaitTermination(600, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}

Threadnumber could be 1 or 8 the result will be the same on my computer. I tested it with Startchar: a inpuLength: 17 withoutStartCharacter : sdf The result will be the same: 135 second My computer: i7-6700HQ with 8 GB of ram (4 core 8 thread)

And the permutation code:

   private static void permutation(String startCharacters, char[] maxLength, int pos, String input) {
    if (pos == maxLength.length) {
        if (leRepetation(maxLength))
            System.out.println(startCharacters + new String(maxLength));
    } else {
        for (int i = 0; i < inputSize; i++) {
            maxLength[pos] = input.charAt(i);
            permutation(startCharacters, maxLength, pos + 1, input);
        }
    }
}

Can I speed up the proccess somehow with Multithreading ? Or any other way to speed it up?

Upvotes: 0

Views: 60

Answers (1)

Joe C
Joe C

Reputation: 15694

This looks like a good candidate for a ForkJoinPool. Within a task, you can fork off various child tasks across multiple threads, which I think is your intention.

Upvotes: 1

Related Questions