I'm building a simple benchmark program that proves the primality the largest 64-bit unsigned prime number. So far, I can set up the threads and they all run correctly, but in an effort to increase the efficiency in which the threads are executed, I want to start all the threads at once. I don't know how to do this, or if it is even possible. Currently I have it loop through an array list of the threads starting each one individually. The number of threads created is your free memory divided by five megabytes (5 * Math.pow(2,10)). Also, am I waiting for termination the right way?
Execution class:
package benchmark; import java.util.ArrayList; import javax.swing.JOptionPane; public class Run { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub long lim = (long)4294967296.0;//upper limit of loop JOptionPane.showMessageDialog(null, "Welcome to the 64-bit prime benchmark. It measures how efficiently your machine can run through the algorithm."); long threads = (Runtime.getRuntime().freeMemory())/(5 * (long)(Math.pow(2, 10)));//calculates the number of threads to use. JOptionPane.showMessageDialog(null, "It will run using " + threads + " threads.\n\nPress OK to build the threads."); ArrayList<Thread> th = new ArrayList<Thread>();//arraylist of threads long inc = lim/threads, s = 2; for(long i = 0; i < threads; i++) { th.add(new Algorithm(s,inc+s));//creates each thread and adds it to the array list } JOptionPane.showMessageDialog(null, "Threads built. \n\nPress OK to run benchmark."); long start = System.currentTimeMillis();//times the amount of time it takes for(int i = 0; i < threads; i++) { th.get(i).start();//starts each thread } while(th.get(th.size()-1).isAlive()) {}//waits for the last thread in the list to terminate long end = System.currentTimeMillis();//end of the timer //prints some stats JOptionPane.showMessageDialog(null,"Statistics:\n" + "Time: " + (end - start) + " ms.\n" + "Total iterations: " + lim/2 + "\n" + "Iterations per second: " + ((lim/2)/(end-start))*1000+"\n" + "Number of threads: "+ th.size()); System.exit(1); } }
Algorithm():
package benchmark; import org.jscience.mathematics.number.LargeInteger; public class Algorithm extends Thread{ /** * @param args */ private LargeInteger val = LargeInteger.valueOf("18446744073709551557");//number being factored private long start, end, increment = 2;//runtime variables that specify where to start, stop, and the increment for each number /** * @param start - value to start at * @param end - value to end at */ public Algorithm(long start, long end) { this.start =start; this.end = end; } public synchronized void run() { //System.out.println("Thread: " + threadNum + " starting. Thread ID: " + getId()); LargeInteger lim = LargeInteger.valueOf(end);//LargeInteger containing the end value for(LargeInteger i = LargeInteger.valueOf(start); lim.isGreaterThan(i); i.plus(increment))//loop for primality check { if(val.mod(i).equals(0))//checks for accuracy of the thread { throw new RuntimeException("Inaccurate Process.");//throughs an exception if an error is made showing the value not prime. } } } }
I will attach the JScience library that I use here.