Threads, Pipes Eratosthenes\' Sieve

Hey, in need of a project in 6 hours?

Threads, Pipes

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.


[10 p.]

Here is the algorithm:

A thread named Generator generates consecutive natural numbers (> 1) (until it reaches the limit given as argument) and writes them to a pipe.

The numbers are received by another thread named Sieve, created by the generator.

The sieving thread stores the first number read from the pipe. It is a prime number.

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.

Then the sieving thread returns (using another pipe) the stored prime number to the thread, from which it received the numbers.

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).

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 the class Generator. An object of this class is a thread generating integer numbers (>1).


Step 2

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


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.



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.

Skills: Java

See more: sieve eratosthenes, sieve, who created prime numbers, which is prime numbers, which is not a prime number, which are prime numbers, using prime numbers, types of algorithm, the sieve of eratosthenes, sieve prime numbers, sieve prime number, sieve prime, sieve of prime numbers, sieve of eratosthenes prime numbers, sieve of eratosthenes c, sieve of eratosthenes, sieve of, sieve numbers, sieve number 4, sieve no, sieve for prime numbers, sieve algorithm in c, sieve algorithm for prime numbers, prime sieve of eratosthenes, prime sieve

About the Employer:
( 0 reviews ) Warsaw, Poland

Project ID: #4505387

8 freelancers are bidding on average $133 for this job


I am Java expert,let me help you!

$173 USD in 0 days
(65 Reviews)

Hi, I am Java expert with experience both in algorithms and parallel programming, I think I am perfect candidate for this job, Thanks

$149 USD in 2 days
(182 Reviews)

Would be my pleasure to work on this project. Kind regards, Rumen

$155 USD in 3 days
(19 Reviews)

Expert in Java.

$55 USD in 2 days
(2 Reviews)

i can help u

$155 USD in 3 days
(0 Reviews)

hi , am a java expert with extensive knowledge of threads , Please check your pm

$111 USD in 1 day
(0 Reviews)

I am expert in Java technology and have good experience in Multi-Threading. I would like to work on this project.

$155 USD in 3 days
(0 Reviews)

Sounds like fun. I would do it for 1/10 of my usual rate just for the fun.

$111 USD in 2 days
(0 Reviews)