Finding prime numbers by threads in java(3)

Osman Yasal
2 min readMay 14, 2021

--

In the SieveActorActor class we define an 1_000 size array. in this array we’ll add the prime values which will be checked for the next given values.

if the given value is locally prime(defined in our array as prime) we’ll simply put it into our array. if it’s locally prime and we don’t have enough room for that, we’ll create another actor after this and pass the value to the actor.

Next actor is going to check the value and put into the local array as prime number.

Because all our SeiveActorActor has no data race, we can create an execute Actors as parallel each other(as PCDP.asnyc(()->{…});

we simply give the top value to countPrimes(..) as input. and we declare a PCDP.finish(()->{…}) operation to make sure all asynchronous Actors are done.

After that, we’ll go through all actors and sum their “length of localPrimes” and print it.

And Here is an execution :

Even though sequantial calculation is optimum for small numbers, when we go to billion and so on, parallel calculation takes less time.

We can implement “Dynamic pipeline with Actors design” to any other problems like dynamic pipeline processing for image processing, data processing etc.

Thanks for reading ❤

--

--

Osman Yasal
Osman Yasal

Written by Osman Yasal

Parallel Computing | Performance tools | Digital Twins | Computer Vision | Image Processing | Deep Learning.

No responses yet