Write a Java program that creates several threads, according to the following scenario: The initial thread will be calle

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Write a Java program that creates several threads, according to the following scenario: The initial thread will be calle

Post by answerhappygod »

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 TOTAL
The goal of this program is to find all the Vampire numbers in
the interval [100.000, 999.999]. To achieve this goal we will scan
all the integer numbers from 100.000 to 999.999 and for each of
these numbers we will 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:
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 [100.000, 999.999] as “The TOTAL number of Vampire
numbers found is: ...” (the ellipsis stand for the actual
number)
The first worker will scan and verify all the even numbers in
the interval [100.000, 999.999]; 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)
The second worker will scan and verify all the odd numbers in
the interval [100.000, 999.999]; 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.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply