1
//////////////////////////////////////////////////////////////////////
3
// JCSP ("CSP for Java") Libraries //
4
// Copyright (C) 1996-2008 Peter Welch and Paul Austin. //
5
// 2001-2004 Quickstone Technologies Limited. //
7
// This library is free software; you can redistribute it and/or //
8
// modify it under the terms of the GNU Lesser General Public //
9
// License as published by the Free Software Foundation; either //
10
// version 2.1 of the License, or (at your option) any later //
13
// This library is distributed in the hope that it will be //
14
// useful, but WITHOUT ANY WARRANTY; without even the implied //
15
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
16
// PURPOSE. See the GNU Lesser General Public License for more //
19
// You should have received a copy of the GNU Lesser General //
20
// Public License along with this library; if not, write to the //
21
// Free Software Foundation, Inc., 59 Temple Place, Suite 330, //
22
// Boston, MA 02111-1307, USA. //
24
// Author contact: P.H.Welch@kent.ac.uk //
27
//////////////////////////////////////////////////////////////////////
30
import org.jcsp.lang.*;
31
import org.jcsp.plugNplay.ints.*;
32
import org.jcsp.demos.util.*;
37
public class Eratosthenes {
39
public static final String TITLE = "Sieve of Eratosthenes";
40
public static final String DESCR =
41
"Demonstrates the pipe-line approach to parallelisation, generating prime numbers by sieving. The " +
42
"sieve of Eratosthenes works by a feeder process pushing numbers into a pipeline of sieve processes. " +
43
"Each sieve process is allocated a prime number and discards any numbers in the pipeline divisible " +
44
"by that number. If a number reaches the end of the pipe it must be prime as no factors were found. " +
45
"It is printed and a new sieve process added to the pipeline to carry on searching for higher primes.\n" +
47
"This demonstration is a good way of stress testing the system for running a lot of processes.";
49
public static void main (String[] args) {
51
Ask.app (TITLE, DESCR);
55
One2OneChannelInt c = Channel.one2oneInt ();
58
new Primes (c.out ()),
59
new PrinterInt (c.in (), "--> ", "\n")