Sahil-Pattni
11/17/2016 - 4:31 AM

HW10 Threads n shit

HW10 Threads n shit

import java.util.concurrent.CountDownLatch;

/**
 * Created by Sahil Pattni on 16-Nov-16.
 */
public class CountPrimesExecutor {
    private static CountDownLatch latch;
    private long lower;
    private long upper;
    private int numThreads;
    private CountPrimes[] countPrimeThreads;

    public CountPrimesExecutor(int numThreads, long lower, long upper)
    {
        this.lower = lower;
        this.upper = upper;
        this.numThreads = numThreads;
        latch = new CountDownLatch(numThreads);
        countPrimeThreads = new CountPrimes[numThreads];
        initializeThreads();
    }

    public void initializeThreads()
    {
        long upper_divisible = (((upper - lower) + 1)/numThreads) + (lower - 1);
        long lower_division = lower;
        if (upper > 0)
        {
            while (upper_divisible <= upper)
            {
                for (int i = 0; i < numThreads; i++)
                {
                    countPrimeThreads[i] = new CountPrimes(lower_division, upper_divisible);
                    upper_divisible += ((upper - lower) + 1)/numThreads;
                    lower_division += ((upper - lower) + 1)/numThreads;

                }
            }
        }
        else
        {
            for (int i = 0; i < numThreads; i++)
            {
                countPrimeThreads[i] = new CountPrimes(lower_division, upper_divisible);
            }
        }
    }

    public void executeThreads()
    {
        for(int i = 0; i < countPrimeThreads.length; i++)
        {
            countPrimeThreads[i].start();
        }
        try {
            for(int i = 0; i < countPrimeThreads.length; i++)
            {
                countPrimeThreads[i].join();
            }

        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    public static void decrementCountDownLatch() {
        latch.countDown();
    }

    public static void main(String[] args) {
        CountPrimesExecutor countPrime = new CountPrimesExecutor(Integer.parseInt(args[0]), Integer.parseInt(args[1]), Integer.parseInt(args[2]));
        countPrime.executeThreads();
    }
}
import java.util.Scanner;

/**
 * Created by Sahil Pattni on 16-Nov-16.
 */
public class CountPrimes extends Thread
{
    private long lower;
    private long upper;
    private static int numPrimes;

    public CountPrimes(long lower, long upper)
    {
        this.lower = lower;
        this.upper = upper;
        numPrimes = 0;
    }

    @Override
    public void run()
    {
        for (long i = lower; i < upper; i++)
        {
            if (isPrime(i))
                numPrimes++;
        }
        try{
            Thread.sleep(500);
        }
        catch (InterruptedException e){
            System.err.print(e.getMessage());
        }
        CountPrimesExecutor.decrementCountDownLatch();
    }

    public static int getNumPrimes()
    {
        return numPrimes;
    }

    public static void resetNumPrimes()
    {
        numPrimes = 0;
    }

    private boolean isPrime(long num)
    {
        long limit = (long) Math.sqrt(num);

        if (num < 2)
            return false;
        else if(num==2||num==3)
            return true;
        else if (num%2==0||num%3==0)
            return false;
        for (long i = 5; i < limit; i+=2)
            {
                if (num % i == 0)
                    return false;
            }


        return true;
    }

}