歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Java 下計算質數的多線程跟單線程執行代碼分析

Java 下計算質數的多線程跟單線程執行代碼分析

public abstract class AbstractPrimeFinder { 
    public boolean isPrime(final int number) { 
        if (number <= 1) 
            return false; 
        for (int i = 2; i <= Math.sqrt(number); i++) 
            if (number % i == 0) 
                return false; 
        return true; 
    } 
 
    public int countPrimesInRange(final int lower, final int upper) { 
        int total = 0; 
        for (int i = lower; i <= upper; i++) 
            if (isPrime(i)) 
                total++; 
        return total; 
    } 
 
    public void timeAndCompute(final int number) { 
        final long start = System.nanoTime(); 
        final long numberOfPrimes = countPrimes(number); 
        final long end = System.nanoTime(); 
        System.out.printf("Number of primes under %d is %d\n", number, 
                numberOfPrimes); 
        System.out.println("Time (seconds) taken is " + (end - start) / 1.0e9); 
    } 
 
    public abstract int countPrimes(final int number); 

 

 

public class SequentialPrimeFinder extends AbstractPrimeFinder { 
    public int countPrimes(final int number) { 
        return countPrimesInRange(1, number); 
    } 
 
    public static void main(final String[] args) { 
        new SequentialPrimeFinder().timeAndCompute(10000000); 
    } 

Copyright © Linux教程網 All Rights Reserved