Simple Java Concurrent Erathostenes Sieve

Hi,
I need following assignment done.
Algorithm and steps are provided so it should be very easy for a java expert.

Eratosthenes’ Sieve

Eratosthenes’ sieve is a method for generating prime numbers. Our goal is to implement it using threads and pipes. The task may occur difficult to do at once, so it is divided into several steps.

Here is the algorithm:

1. A thread named Generator generates consecutive natural numbers (> 1) (until it reaches the limit given as argument) and writes them to a pipe.
2. The numbers are received by another thread named Sieve, created by the generator.
3. The sieving thread stores the first number read from the pipe. It is a prime number.
4. Afterwards it creates new sieving thread and transfers to it (using another pipe) the rest of the received numbers except for numbers divisible by the stored prime number.
5. Then the sieving thread returns (using another pipe) the stored prime number to the thread, from which it received the numbers.
6. Next, the sieving thread reads prime numbers from the (another, not used yet) pipe connecting it and the sieving thread it created earlier and sends them to its parent (the thread, to which the stored prime was sent; it is the thread, that created this one).
7. Having generated all the natural numbers, the main thread – the Generator – starts reading prime numbers from the pipe connecting it and its descendant and writes them to the command prompt.

Step 1

Create a class named Generator. An object of this class is a thread generating integer numbers (>1).
Step 2

Create a class named Sieve. An object of this class is a thread reading numbers from the pipe connecting it to the generator.

Notes:

* A stream object must be passed to the receiver thread as an argument to a constructor.
* A pipe stream must be decorated with a data stream (DataOutputStream or DataInputStream) to enable transfer of primitive types (integers).
* Having printed all the received numbers, both threads should terminate cleanly. To achieve this, the special value 0 may be sent as a termination signal (EOD, end of data).

Step 3

The receiver thread passes all the received numbers back to the sender using another pipe.
The sender (the generator) prints them out to the command prompt.
Step 4

* The sieving thread creates another sieving thread and transfers to it all the numbers it has received.
* The created sieving thread (descendant) reads the numbers and sends them back to the sender immediately (using another pipe).
* The sieving thread created by the generator receives the numbers from the descendant and returns them to the generator, who prints them.

There are 3 threads here.
Step 5, the last

The sieving thread does not pass all the numbers it gets to the descendant. It passes only those of them that are not divisible by the first number it got (and stored).

Each sieving thread creates another sieving thread until there are any numbers left in the transferred sequence.

Besides the stored prime number, it passes to the parent thread all the numbers received from the descendant.
Notes:

* Each sieving thread must take 2 stream objects as arguments to its constructor (only in the final version).
* All the streams must be closed properly.
* Each thread should print lots of diagnostic messages to ease debugging. However, a correctly working program should print only the prime numbers.

Leave a Reply

Your email address will not be published. Required fields are marked *