I WROTE THE FOLLOWING CODE FOR COMPUTE THE FACTORIAL VALUE OF A NUMBER IN (SYNCHRONIZED MODE) BUT THERE IS A PROBLEM, IT PRINT THE FACTORIAL VALUE OF AS ZERO. COULD ANY ONE HELP TO FIGURE OUT THE PROBLEM AND THE CORRECT CODE ? import java.util.concurrent. ExecutorService; import java.util.concurrent.Executors; public class Fact { final static int THREAD COUNT =2; //Runtime.getRuntime().availableProcessors() static int n = 8; static int fact = 1; public static void main(String[] args) { %3! // without sychnoriouse

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
i need the answer quickly
I WROTE THE FOLLOWING CODE FOR COMPUTE THE FACTORIAL VALUE OF
A NUMBER IN (SYNCHRONIZED MODE) BUT THERE IS A PROBLEM, IT PRINT
THE FACTORIAL VALUE OF AS ZERO .COULD ANY ONE HELP TO FIGURE
OUT THE PROBLEM AND THE CORRECT CODE ?
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Fact {
final static int THREAD_COUNT =2; //Runtime.getRuntime().availableProcessors ()
static intn = 8;
static int fact = 1;
public static void main(String[] args) {
// without sychnoriouse
ExecutorService executor = Executors.newCachedThreadPool ();
long startTime = System.currentTimeMillis();
for (int th = 0; th < THREAD_COUNT; th++) {
executor.execute(new FactCom(th));
}
executor.shutdown ();
// Wait until all tasks are finished
while (!executor.isTerminated()) {
}
long endTime = System.currentTimeMillis();
|//result almost incorrect with out Synchronization
System.out. println("FACT for "+ n +" :"+ fact);
System.out.println("Number of threads is " + THREAD_COUNT);
System.out.println("Time with " + (endTime startTime) +
%3D
milliseconds");
}
static class FactCom implements Runnable {
int threadRank;
FactCom(int threadRank) {
this.threadRank = threadRank;
}
public void run() {
threadFact (threadRank);
}
void threadFact(int threadRank) {
int my_n = n / THREAD_COUNT;
int my_first_i = my_n * threadRank;
int my_last_i = my_first_i + my_n;
for(int i = my_first_i; i <my_last_i; i++)
{
fact=fact*i;
}
}
Transcribed Image Text:I WROTE THE FOLLOWING CODE FOR COMPUTE THE FACTORIAL VALUE OF A NUMBER IN (SYNCHRONIZED MODE) BUT THERE IS A PROBLEM, IT PRINT THE FACTORIAL VALUE OF AS ZERO .COULD ANY ONE HELP TO FIGURE OUT THE PROBLEM AND THE CORRECT CODE ? import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Fact { final static int THREAD_COUNT =2; //Runtime.getRuntime().availableProcessors () static intn = 8; static int fact = 1; public static void main(String[] args) { // without sychnoriouse ExecutorService executor = Executors.newCachedThreadPool (); long startTime = System.currentTimeMillis(); for (int th = 0; th < THREAD_COUNT; th++) { executor.execute(new FactCom(th)); } executor.shutdown (); // Wait until all tasks are finished while (!executor.isTerminated()) { } long endTime = System.currentTimeMillis(); |//result almost incorrect with out Synchronization System.out. println("FACT for "+ n +" :"+ fact); System.out.println("Number of threads is " + THREAD_COUNT); System.out.println("Time with " + (endTime startTime) + %3D milliseconds"); } static class FactCom implements Runnable { int threadRank; FactCom(int threadRank) { this.threadRank = threadRank; } public void run() { threadFact (threadRank); } void threadFact(int threadRank) { int my_n = n / THREAD_COUNT; int my_first_i = my_n * threadRank; int my_last_i = my_first_i + my_n; for(int i = my_first_i; i <my_last_i; i++) { fact=fact*i; } }
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY