Finding prime numbers by threads in java(2)
In this article i would like to mention about how to find prime numbers with threads asynchronously but first i’m gonna quick recap an easy concept which is called Actors
By finding prime numbers we’ll create a pipeline dynamically and to do so we’re gonna use actors.
You may simply think Actors as a small part of a pipeline. every actors have a process(Object obj) method which takes the “parallelised data” as an input. After processing the given data, some data will be send to another actor to process like as a pipeline.
Here’s a basic demonstration about how actors work
Every actor has a set of rules that will process the data according to. if a data doesn’t fit the defined set of rules, it will be delivered to next actor to be processed and so on.
Because every actors has it’s own rules and distinct data, there’s no data racing. which means we can execute all of them asynchronously.
in our example our dynamic pipeline might be as below
The first thing that you must remember is; this is a dynamic pipeline, so at T0 we only have Actor(0) and we’ll create other actors if we need to.
First actor checks if the given number (2 to 129 respectively) is prime ( it’s prime only if it’s not fully divided by 2,3 or 5) even though 2,3,5 is prime continuing prime values like 7 , 11.. will be passed to another actor to look at.