Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.math.BigDecimal;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Iterator;
- import java.util.concurrent.ConcurrentLinkedQueue;
- import java.util.concurrent.CountDownLatch;
- public class Primer2 {
- public Integer stall = 1;
- private ConcurrentLinkedQueue<Integer> numbs;
- public long start;
- public Primer2() {
- numbs = new ConcurrentLinkedQueue<Integer>();
- }
- // ///////////////////////////////////////////////////////
- class Task implements Runnable {
- private int[] numbs;
- //private ArrayList<Integer> numbs;
- private int myIndex;
- private CountDownLatch lat;
- private Runtime runtime;
- //private ArrayList<Integer> al;
- private int[] al;
- private final int s = 32343;
- private Task() {
- }
- public Task(ConcurrentLinkedQueue<Integer> numbs, CountDownLatch lat,
- Runtime runtime) {
- Integer[] temp = new Integer[0];
- temp = numbs.toArray(temp);
- this.numbs = new int[temp.length];
- for (int index = 0; index < temp.length; index++) {
- this.numbs[index] = temp[index];
- }
- //this.numbs = new ArrayList<Integer>(numbs);
- this.myIndex = 0;
- this.lat = lat;
- this.runtime = runtime;
- this.al = new int[s];
- //this.al = new ArrayList<Integer>(s);
- }
- public boolean isPrime (int num) {
- //ArrayList<Integer> al = new ArrayList<Integer>(s);
- //al.clear();
- for (int c = 0; c < s; c++) {
- //al.add(c);
- al[c] = c;
- }
- //Iterator<Integer> it = al.iterator();
- //if (it.hasNext()) {
- // int c = it.next();
- // c = c++;
- //}
- for (int c : al) {
- c++;
- }
- boolean rv = true;
- if (num > 2) {
- for (int d = 2; d < num; d++) {
- if (num % d == 0) {
- rv = false;
- break;
- }
- }
- } else if (num < 2) {
- rv = false;
- }
- return rv;
- }
- public void run () {
- //stall++;
- long myStart = System.nanoTime();
- //int totalNum = numbs.size();
- int totalNum = numbs.length;
- while ( myIndex < totalNum) {
- //isPrime(numbs.get(myIndex));
- isPrime(numbs[myIndex]);
- myIndex++;
- }
- long end = System.nanoTime();
- runtime.setRuntime( (end - myStart) / 10e5);
- lat.countDown();
- }
- }
- class Runtime {
- private double runtime;
- public double getRuntime () {
- return runtime;
- }
- public void setRuntime (double runtime) {
- this.runtime = runtime;
- }
- }
- // /////////////////////////////////////////////////
- public static void main(String[] args) {
- System.out.println("------------------start------------------");
- int k = 1;
- if (args.length > 0) {
- k = Integer.valueOf(args[0]);
- }
- CountDownLatch lat = new CountDownLatch(k);
- Primer2 p = new Primer2();
- for (int i = 0; i < 100000; i++) {
- p.numbs.add(i);
- }
- ConcurrentLinkedQueue<Integer>[] qjobs = (ConcurrentLinkedQueue<Integer>[]) new ConcurrentLinkedQueue[k];
- for (int c = 0; c < k; c++) {
- qjobs[c] = new ConcurrentLinkedQueue<Integer>();
- }
- int i = 0;
- for (int n : p.numbs) {
- qjobs[i++ % k].add(n);
- }
- Thread[] ts = new Thread[k];
- Runtime[] runtimes = new Runtime[k];
- for (int c = 0; c < k; c++) {
- runtimes[c] = p.new Runtime();
- ts[c] = new Thread(p.new Task(qjobs[c], lat, runtimes[c]));
- }
- p.start = System.nanoTime();
- for (int c = 0; c < k; c++) {
- ts[c].start();
- }
- try {
- lat.await();
- long end = System.nanoTime();
- for (int c = 0; c < k; c++) {
- ts[c].interrupt();
- }
- System.out.println(k + " threads' runtimes:");
- double[] rt = new double[k];
- for (int c = 0; c < k; c++) {
- rt[c] = (new BigDecimal(runtimes[c].getRuntime()).setScale(0,
- BigDecimal.ROUND_HALF_UP)).doubleValue();
- System.out.println( (c + 1) + "\t\t" + rt[c]);
- }
- Arrays.sort(rt);
- System.out.println("maximum:\t" + rt[k - 1]);
- System.out.println("main time:\t"
- + (new BigDecimal( (end - p.start) / 10e5).setScale(
- 0, BigDecimal.ROUND_HALF_UP))
- .doubleValue());
- System.out.println("------------------end------------------");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement