Java字符串置换执行器服务

Java字符串置换执行器服务,java,string,algorithm,permutation,Java,String,Algorithm,Permutation,我希望使用ExecutorService打印所有字符串排列,但与在单个线程上运行它相比,它并没有更快 我的代码: private static void setUpThreads(String startCharacter, char inputLength[], String withoutStartChar, int threadnumber) { ExecutorService exec = Executors.newFixedThreadPool(threadnumber);

我希望使用ExecutorService打印所有字符串排列,但与在单个线程上运行它相比,它并没有更快

我的代码:

  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可以是1或8。在我的计算机上,结果将是相同的。 我用Startchar:a输入长度:17,外部字符:sdf测试了它 结果是一样的:135秒 我的电脑:i7-6700HQ,8 GB内存4核8线程

以及排列代码:

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

我可以用多线程加速进程吗?或者其他加速的方法?

这看起来是一个很好的选择。在一个任务中,您可以跨多个线程完成各种子任务,我认为这是您的意图。

没有多线程的速度是多少?秒?分钟?135秒没有ExecutorService,135秒有ExecutorService 8线程你知道你只提交了一个任务,对吗?因此,执行器中的所有其他线程都处于空闲状态,但您只创建了一个在一个线程中执行的任务。我明白了,为什么你认为时间会不同呢。我可以用多线程加速这个任务吗?