|
| 1 | +package org.threadly.examples.prime; |
| 2 | + |
| 3 | +import java.math.BigInteger; |
| 4 | +import java.util.ArrayDeque; |
| 5 | +import java.util.Deque; |
| 6 | +import java.util.Iterator; |
| 7 | +import java.util.concurrent.Callable; |
| 8 | +import java.util.concurrent.ExecutionException; |
| 9 | +import org.threadly.concurrent.PriorityScheduler; |
| 10 | +import org.threadly.concurrent.future.ListenableFuture; |
| 11 | +import org.threadly.util.ExceptionUtils; |
| 12 | + |
| 13 | +/** |
| 14 | + * <p>Tester which takes a (ideally) very large number as an argument, and in parallel determines |
| 15 | + * if it is prime or not.</p> |
| 16 | + * |
| 17 | + * @author jent - Mike Jensen |
| 18 | + */ |
| 19 | +public class NextPrime { |
| 20 | + @SuppressWarnings("javadoc") |
| 21 | + public static void main(final String args[]) throws InterruptedException { |
| 22 | + if (args.length == 0) { |
| 23 | + System.err.println("No number to test provided"); |
| 24 | + System.err.println("Usage: java -cp threadly_examples.jar " + |
| 25 | + NextPrime.class.getName() + " [number to start search from]..."); |
| 26 | + System.exit(1); |
| 27 | + } |
| 28 | + |
| 29 | + final int processingThreads = Runtime.getRuntime().availableProcessors() * 2; |
| 30 | + int threadPoolSize = processingThreads + args.length - 1; |
| 31 | + final PriorityScheduler executor = new PriorityScheduler(threadPoolSize, true); |
| 32 | + executor.prestartAllThreads(); |
| 33 | + |
| 34 | + Deque<ListenableFuture<PrimeResult>> futures = new ArrayDeque<ListenableFuture<PrimeResult>>(processingThreads * 2); |
| 35 | + |
| 36 | + int value = Integer.parseInt(args[0]) + 1; |
| 37 | + while (true) { |
| 38 | + final int f_i = value++; |
| 39 | + futures.add(executor.submit(new Callable<PrimeResult>() { |
| 40 | + @Override |
| 41 | + public PrimeResult call() throws InterruptedException { |
| 42 | + return testNumber(executor, processingThreads, f_i); |
| 43 | + } |
| 44 | + })); |
| 45 | + |
| 46 | + boolean firstRun = true; |
| 47 | + while (firstRun || futures.size() > processingThreads * 2) { |
| 48 | + firstRun = false; |
| 49 | + Iterator<ListenableFuture<PrimeResult>> it = futures.iterator(); |
| 50 | + while (it.hasNext()) { |
| 51 | + ListenableFuture<PrimeResult> f = it.next(); |
| 52 | + if (f.isDone()) { |
| 53 | + try { |
| 54 | + PrimeResult result = f.get(); |
| 55 | + if (result.isPrime) { |
| 56 | + System.out.println("The next prime number is: " + result.testNumber); |
| 57 | + System.exit(0); |
| 58 | + } else { |
| 59 | + it.remove(); |
| 60 | + } |
| 61 | + } catch (ExecutionException e) { |
| 62 | + throw ExceptionUtils.makeRuntime(e.getCause()); |
| 63 | + } |
| 64 | + } else { |
| 65 | + break; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + Thread.sleep(10); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private static PrimeResult testNumber(PriorityScheduler executor, |
| 75 | + int threadCount, int number) throws InterruptedException { |
| 76 | + // TODO - improve this with an AKS implementation |
| 77 | + PrimeProcessor primeProcessor = new DumbTester(new BigInteger(Integer.toString(number))); |
| 78 | + return new PrimeResult(number, primeProcessor.isPrime(executor, threadCount)); |
| 79 | + } |
| 80 | + |
| 81 | + private static class PrimeResult { |
| 82 | + public final int testNumber; |
| 83 | + public final boolean isPrime; |
| 84 | + |
| 85 | + public PrimeResult(int testNumber, boolean isPrime) { |
| 86 | + this.testNumber = testNumber; |
| 87 | + this.isPrime = isPrime; |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments