1 /* 2 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * - Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 11 * - Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * - Neither the name of Oracle nor the names of its 16 * contributors may be used to endorse or promote products derived 17 * from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * This source code is provided to illustrate the usage of a given feature 34 * or technique and has been deliberately simplified. Additional steps 35 * required for a production-quality application, such as security checks, 36 * input validation and proper error handling, might not be present in 37 * this sample code. 38 */ 39 package stream.parallel; 40 41 import java.math.BigInteger; 42 import java.util.concurrent.ThreadLocalRandom; 43 import java.util.stream.IntStream; 44 45 /** 46 * This demo shows how to use the parallel mode to find several number for given 47 * length prime number. The process easily use a parallel stream to generate 48 * a number of desired length probable prime number. Following with filter 49 * operation to make sure we will get desirable length prime numbers. 50 * 51 * @author tyan 52 */ 53 public class ProbablePrimeNumber { 54 /** 55 * Try to get length from command line. Use the length to create random long 56 * stream, that integer's digit is length. 57 * 58 * @param args This has to be a 1-element or 2-elements array, The first 59 * element must be a positive number, which equals to length of generated 60 * prime number stream. Second element is optional, it will be used as how 61 * many probable prime number that will be generated if it's set. 62 */ 63 private static int DEFAULT_NUM = 1 << 10; 64 65 public static void main(String[] args) { 66 if (args.length != 1 && args.length != 2) { 67 usage(); 68 return; 69 } 70 try { 71 int expectedLength = Integer.parseInt(args[0]); 72 int size = (args.length == 2) ? 73 Integer.parseInt(args[1]) : 74 DEFAULT_NUM; 75 IntStream.range(0, size). 76 parallel(). 77 mapToObj(i -> 78 BigInteger.probablePrime(expectedLength * 10 / 3 - 79 ThreadLocalRandom.current().nextInt(3), 80 ThreadLocalRandom.current())). 81 filter(c -> c.toString().length() == expectedLength). 82 forEach(prime -> 83 System.out.println("Found a probable prime number :" 84 + prime)); 85 } catch (NumberFormatException nfe) { 86 usage(); 87 } 88 } 89 90 /** 91 * Usage of this program 92 */ 93 public static void usage() { 94 System.out.println("Usage: java ProbablePrimeNumber lentgh [size]"); 95 System.out.println("size is a positive integer"); 96 } 97 }