java - Multi-threading same code at the same time -
i want programme test 2 biginteger
s @ same time. of code testing 1 @ time. since want code same both runs, there simple synchronized statement able utilize implement this?
public static void main(string[] args) { string input = joptionpane.showinputdialog("enter number "); int number = integer.parseint(input); biginteger num = new biginteger(input); string output = num + " is" + (isprime(num) ? " " : " not ") + "a prime number."; joptionpane.showmessagedialog(null, output); } public static boolean isprime(biginteger num) { if (num.mod(new biginteger("2")).compareto(biginteger.zero) == 0) { homecoming false; (biginteger = new biginteger("3"); i.multiply(i).compareto(num) <= 0; = i.add(new biginteger("2"))) { if (num.mod(i).compareto(biginteger.zero) == 0) { homecoming false; } } homecoming true; } }
i have reached solution question.
because 2 integers beingness tested needed create textone , texttwo test each individually primeness.
public static boolean isprime(biginteger textone) { // check if number multiple of 2 if (textone.mod(new biginteger("2")).compareto(biginteger.zero) == 0) { homecoming false; }// if not, check odds (biginteger = new biginteger("3"); i.multiply(i).compareto(textone) <= 0; = i.add(new biginteger("2"))) { if (textone.mod(i).compareto(biginteger.zero) == 0) { homecoming false; } } homecoming true; } public static boolean isprime2(biginteger texttwo) { // check if number multiple of 2 if (texttwo.mod(new biginteger("2")).compareto(biginteger.zero) == 0) { homecoming false; }// if not, check odds (biginteger = new biginteger("3"); i.multiply(i).compareto(texttwo) <= 0; = i.add(new biginteger("2"))) { if (texttwo.mod(i).compareto(biginteger.zero) == 0) { homecoming false; } } homecoming true; }
i designated num , num2 designated bigintegers output reflect following..
public void run() { string output = num + " is" + (isprime(num) ? " " : " not ") + "a prime number."; lblresults.settext(output); string output2 = num2 + " is" + (isprime(num2) ? " " : " not ") + "a prime number."; lblresults2.settext(output2); }}
java multithreading joptionpane biginteger
No comments:
Post a Comment