Threads in Java
Write a Java program that creates several threads, according to the following scenario:
The initial thread will be called the main thread (M)
The main thread (M) creates and starts two worker threads; each worker thread will work on its task
The main thread (M) joins the two worker threads in the end and computes the final result
The goal of this program is to find all the Vampire numbers in the interval [100000, 999999]. To achieve this
goal we will scan all the integer numbers from 100000 to 999999 and for each of these numbers we must
perform a test to verify if that number is a Vampire number or not. In order to solve this problem faster,
(assuming we have at least two processors on our system) we will divide the work between the two worker
threads: on worker will scan and verify all the even numbers and the other worker will scan and verify all the
odd numbers in the interval.
More precisely, the following list describes the behavior of each thread:
1. The main thread (M) creates the two worker threads, starts them and joins them in the end. After that,
the main thread will compute and display the TOTAL number of Vampire numbers found in the interval
[100000, 999999] as “The TOTAL number of Vampire numbers found is: …” (the ellipsis stand for the
actual number)
2. The first worker will scan and verify all the even numbers in the interval [100000, 999999]; whenever a
new Vampire number is found, it will be displayed like this: “First worker found: …” (the ellipsis stand
for the actual number); a counter will be incremented every time a new Vampire number was found,
and in the end the total number of Vampire numbers found will be displayed: “First worker found …
Vampire numbers” (the ellipsis stand for the actual number)
3. The second worker will scan and verify all the odd numbers in the interval [100000, 999999]; whenever
a new Vampire number is found, it will be displayed like this: “Second worker found: …” (the ellipsis
stand for the actual number); a counter will be incremented every time a new Vampire number was
found, and in the end the total number of Vampire numbers found will be displayed: “Second worker
found … Vampire numbers” (the ellipsis stand for the actual number)
HINTS:
– The Vampire numbers are described in detail here: https://en.wikipedia.org/wiki/Vampire_number
– In the end, the worker threads must communicate their counters to the main thread, in order to
compute and display the TOTAL number of Vampire numbers in the interval
– Work on your computer or online using https://replit.com
DELIVERABLES:
– The source code for the Java program, stored in the text file “hw3.java”
– A screenshot showing the final results of your program’s execution, stored in a graphic file like
“hw3.png” or ”hw3.gif” or “hw3.pdf”, etc.