Question
Explain why the following program prints different totals for the given array. Rewrite it so that this does not happen. class ShowInterf { public static
Explain why the following program prints different totals for the given array. Rewrite it so that this does not happen.
class ShowInterf { public static void main(String args[]) { int[] b = { 23, 45, 33, 13, 28, 45, 34, 54, 67 }; (new Thread(new Summer(b))).start(); (new Thread(new Swapper(b))).start(); } } class Swapper implements Runnable { int b[]; public Swapper(int k[]) { b = k; } public void run() { int j, k, t; while (true) { j = (int)(Math.random() * b.length); k = (int)(Math.random() * b.length); t = b[j]; b[j] = b[k]; b[k] = t; } } class Summer implements Runnable { int b[]; public Summer(int k[]) { b = k; } public void run() { while (true) { int total = 0; for (int i = 0; i < b.length; i++) total += b[i]; System.out.println(total); } } }
Update: This is the full question that was provided. Further basic knowledge is not known.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started