[英]Parallel prime number checker using threads in Java
我有一個 Java 程序來檢查數字是否為素數。 我試圖通過使用單獨的線程檢查不同的數字來使其並行。
我運行了程序並比較了它們的執行時間,但我沒有看到改進......
這是我的順序程序,primeSeq.java:
import java.io.*;
import java.text.ParseException;
import java.util.concurrent.TimeUnit;
import java.lang.Object;
class primeSeq {
static boolean isPrime(long n) {
// Check base cases:
// n < 2, n is 2 or 3, n is divisible by 2 or 3
if(n < 2) return false;
if(n == 2 || n == 3) return true;
if(n%2 == 0 || n%3 == 0) return false;
// Check if divisible by all numbers 6k +-1 up to sqrt(n)
long sqrtN = (long)Math.sqrt(n)+1;
for(long i = 6L; i <= sqrtN; i += 6) {
if(n%(i-1) == 0 || n%(i+1) == 0) return false;
}
return true;
}
public static void main(String args[]) throws ParseException
{
if (args.length == 0){
System.out.println("No args provided.");
}
else
{
long startTime = System.nanoTime();
for(int i=0;i< args.length;i++)
{
long single_startTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
boolean isPrime = isPrime(Long.parseLong(args[i]));
long single_endTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
System.out.println(args[i] + ": " + isPrime + "\tStart time: " + single_startTime + "\tEnd time: " + single_endTime + "\tElapsed time: " + (single_endTime - single_startTime));
// boolean isPrime = isPrime(Long.parseLong(args[i]));
// System.out.println(args[i] + ": " + isPrime);
}
long endTime = System.nanoTime();
System.out.println("Total time: " + TimeUnit.NANOSECONDS.toMillis(endTime - startTime) + " milliseconds");
}
}
}
這是我嘗試並行化它,primePar.java:
import java.io.*;
import java.text.ParseException;
import java.util.concurrent.TimeUnit;
import java.lang.Object;
class MyThread extends Thread
{
boolean isPrime(long n) {
// Check base cases:
// n < 2, n is 2 or 3, n is divisible by 2 or 3
if(n < 2) return false;
if(n == 2 || n == 3) return true;
if(n%2 == 0 || n%3 == 0) return false;
// Check if divisible by all numbers 6k +-1 up to sqrt(n)
long sqrtN = (long)Math.sqrt(n)+1;
for(long i = 6L; i <= sqrtN; i += 6) {
if(n%(i-1) == 0 || n%(i+1) == 0) return false;
}
return true;
}
String threadName;
public MyThread(String threadName)
{
super(threadName);
this.threadName = threadName;
}
@Override
public void run()
{
long startTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
boolean isPrime = isPrime(Long.parseLong(threadName));
long endTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
System.out.println(threadName + ": " + isPrime + "\tStart time: " + startTime + "\tEnd time: " + endTime + "\tElapsed time: " + (endTime - startTime));
}
}
class primePar {
public static void main(String args[]) throws ParseException
{
if (args.length == 0){
System.out.println("No args provided.");
}
else
{
long startTime = System.nanoTime();
for(int i=0;i< args.length;i++)
{
try
{
Thread newThread = new MyThread(args[i]);
newThread.start();
newThread.join();
}
catch (InterruptedException exc)
{
System.out.println(exc);
}
}
long endTime = System.nanoTime();
System.out.println("Total time: " + TimeUnit.NANOSECONDS.toMillis(endTime - startTime) + " milliseconds");
}
}
}
這是這些的輸出:
PrimeSeq:
$ java primeSeq 1000000000000037 1000000000000091 1000000000000159 1000000000000187
1000000000000037: true Start time: 143773080 End time: 143773182 Elapsed time: 102
1000000000000091: true Start time: 143773183 End time: 143773284 Elapsed time: 101
1000000000000159: true Start time: 143773284 End time: 143773400 Elapsed time: 116
1000000000000187: true Start time: 143773400 End time: 143773510 Elapsed time: 110
Total time: 430 milliseconds
PrimePar:
$ java primePar 1000000000000037 1000000000000091 1000000000000159 1000000000000187
1000000000000037: true Start time: 143746202 End time: 143746309 Elapsed time: 107
1000000000000091: true Start time: 143746310 End time: 143746415 Elapsed time: 105
1000000000000159: true Start time: 143746415 End time: 143746519 Elapsed time: 104
1000000000000187: true Start time: 143746520 End time: 143746619 Elapsed time: 99
Total time: 418 milliseconds
看來我的並行程序實際上並沒有並行運行。 我怎樣才能解決這個問題?
正如用戶“達伍德說恢復莫妮卡”在評論中指出的那樣, newThread.join()
是有問題的命令。
刪除它會有所幫助,因為所有線程同時啟動:
更新了 primePar.java 程序:
import java.io.*;
import java.text.ParseException;
import java.util.concurrent.TimeUnit;
import java.lang.Object;
class MyThread extends Thread
{
boolean isPrime(long n) {
// Check base cases:
// n < 2, n is 2 or 3, n is divisible by 2 or 3
if(n < 2) return false;
if(n == 2 || n == 3) return true;
if(n%2 == 0 || n%3 == 0) return false;
// Check if divisible by all numbers 6k +-1 up to sqrt(n)
long sqrtN = (long)Math.sqrt(n)+1;
for(long i = 6L; i <= sqrtN; i += 6) {
if(n%(i-1) == 0 || n%(i+1) == 0) return false;
}
return true;
}
String threadName;
public MyThread(String threadName)
{
super(threadName);
this.threadName = threadName;
}
@Override
public void run()
{
long startTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
boolean isPrime = isPrime(Long.parseLong(threadName));
long endTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
System.out.println(threadName + ": " + isPrime + "\tStart time: " + startTime + "\tEnd time: " + endTime + "\tElapsed time: " + (endTime - startTime));
}
}
class primePar {
public static void main(String args[]) throws ParseException
{
if (args.length == 0){
System.out.println("No args provided.");
}
else
{
long startTime = System.nanoTime();
for(int i=0;i< args.length;i++)
{
// try
// {
Thread newThread = new MyThread(args[i]);
newThread.start();
// newThread.join();
// }
// catch (InterruptedException exc)
// {
// System.out.println(exc);
// }
}
long endTime = System.nanoTime();
System.out.println("Total time: " + TimeUnit.NANOSECONDS.toMillis(endTime - startTime) + " milliseconds");
}
}
}
新輸出:
$ java primePar 1000000000000037 1000000000000091 1000000000000159 1000000000000187
Total time: 0 milliseconds
1000000000000091: true Start time: 144449191 End time: 144449354 Elapsed time: 163
1000000000000159: true Start time: 144449191 End time: 144449355 Elapsed time: 164
1000000000000187: true Start time: 144449191 End time: 144449357 Elapsed time: 166
1000000000000037: true Start time: 144449191 End time: 144449370 Elapsed time: 179
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.