我創建了一個程序,爲我的11年級計算機科學項目,我做了一個java密碼破解蠻力的密碼。不過,我想獲得一些關於如何對我的java程序(下面提供的代碼)進行多線程加速的建議,以加速蠻力程序。如果它有幫助,我可以使用英特爾的i7-3770處理器,它是四核處理器,但每個內核有2個線程,因此可以同時處理8個可能的線程。如何多線程的蠻力java密碼程序
下面是代碼:
import java.util.*;
import java.io.*;
class pwcracker
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
Random rand = new Random();
Runtime.getRuntime().availableProcessors();
String pw, choices, guess;
long tries;
int j, length;
System.out.println("Enter a password that is up to 5 chars and contains no numbers: ");
pw = "" + scan.nextLine();
length = pw.length();
choices = "abcdefghijklmnopqrstuvwxyz";
tries = 0;
guess = "";
System.out.println("Your pw is: " + pw);
System.out.println("The length of your pw is: " + length);
System.out.println("for TEST- Guess: " + guess + "pw :"+pw);
if (guess != pw){
while (guess != pw)
{
j = 0;
guess = "";
while (j < length)
{
guess = guess + choices.charAt(rand.nextInt (choices.length()));
j = j + 1;
if (guess == pw)
{
System.out.println("Match found, ending loop..");
break;
}
}
System.out.println("2 Guess: " + guess + " pw :"+pw);
tries = tries + 1;
}
}
System.out.println("Here is your password: " + guess);
System.out.println("It took " + tries + " tries to guess it.");
}
}
你不能使用==比較java中的字符串。你需要使用string1.equals(string2) – slipperyseal
程序不是超線程的,處理器是超線程的。處理器線程與軟件線程沒有任何關係。 – immibis
是的抱歉,我忘了提及我更新了代碼在我的最後使用.equals而不是==所以這不是一個錯誤 –