Anda di halaman 1dari 3

package application;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;

public class Main {

public static void main(String[] args) {

double[] list = new double[1<<20];


for (int i=0; i<list.length; i++)
list[i] = Math.random();
long startTime = System.nanoTime();
int count = bigNums(list);
long endTime = System.nanoTime();
System.out.println("Running time = " +
((endTime-startTime)/1000));

System.out.println("Number Greater than 0.5 are(Using baisc


version) : " + count);
}

private static int bigNums(double[] list) {


double myBigNum []=
{0.6,0.2,0.3,0.6,0.8,0.9,0.4,0.7,0.3,0.1,0.7,0.8,0.9,0.6,0.7,0.9};

FindThread [] threads = new FindThread[8];// create 8


threads
// pass the array
int i=0; //indecies
threads[0] = new
FindThread("threads["+i+++"]",myBigNum,0,1);
threads[1] = new
FindThread("threads["+i+++"]",myBigNum,2,3);
threads[2] = new
FindThread("threads["+i+++"]",myBigNum,4,5);
threads[3] = new
FindThread("threads["+i+++"]",myBigNum,6,7);
threads[4] = new
FindThread("threads["+i+++"]",myBigNum,8,9);
threads[5] = new
FindThread("threads["+i+++"]",myBigNum,10,11);
threads[6] = new
FindThread("threads["+i+++"]",myBigNum,12,13);
threads[7] = new
FindThread("threads["+i+++"]",myBigNum,14,15);

for(int j=0;j<8;j++)
threads[j].start();

try {
for(int k=0;k<8;k++)
threads[k].join();
} catch (InterruptedException e) {}

return FindThread.count;
}
}

package application;

import java.util.ArrayList;

public class FindThread extends Thread {

ArrayList<Double> foundBigs ;
double[] basicArray;
static int count;
int start,end;

FindThread(String name,double[] myBigNum,int start0,int end0){


super(name);
foundBigs = new ArrayList<>();
basicArray = myBigNum;
start = start0;
end= end0;
count=0;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+ "
indeces start= "+start+" end= "+end);
// find a numbre greated than 0.5
for(int i=start;i<=end;i++) {
if(basicArray[i] > 0.5){
count++;
}
}

}
}

OUTPUT;