Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

DO NOT USE SINGLE QUOTES OR DOUBLE QUOTES OR PARENTHESES IN YOUR ANSWERS! (a) Wh

ID: 3536503 • Letter: D

Question

DO NOT USE SINGLE QUOTES OR DOUBLE QUOTES OR PARENTHESES IN YOUR ANSWERS!

(a) Why cant br (declared in line 8) be static and initialized in main?

(b) What lines have potential race conditions?

(c) What would the consequences be of omitting line 43?

(d) There are two sums computed here. Which computation is more efficient? Why?

(e) What would the consequences be of omitting line 41?

(f) Why cant we use "this" in place of datafile in line 22?

(g) How could the synchronized block in lines 22-24 be made more efficient?

(h) What is the point of the conditional in line 21?




Look at this code and answer the questions in the submission box. DO NOT DELETE OR MODIFY THE QUESTIONS! DO NOT USE QUOTES OR PARENTHESES IN WHAT YOU WRITE.

     01        import java.io.*;
     02       
     03        public class ST0 extends Thread {
     04                static String datafile;
     05                static String synch2 = "synch2";
     06                static int sum1=0, sum2=0, numThreads;
     07                private int myNum;
     08                private BufferedReader br;
     09                private String myName() {return Thread.currentThread().getName();}
     10                private ST0(int numThreads, int myNum) throws IOException {
     11                        this.myNum = myNum;
     12                        br = new BufferedReader(
     13                                new InputStreamReader(new FileInputStream(datafile)));
     14                }
     15                public void run() {
     16                        try {
     17                                String line = br.readLine();
     18                                int mysum = 0;
     19                                int count=0;
     20                                while (line!=null) {
     21                                        if (count%numThreads==myNum) {
     22                                                synchronized(datafile) {
     23                                                        sum1 += Integer .parseInt(line.trim());
     24                                                }
     25                                                mysum += Integer .parseInt(line.trim());
     26                                        }
     27                                        count++;
     28                                        line = br.readLine();
     29                                }
     30                                synchonized(synch2) {
     31                                        sum2 += mysum;
     32                                }
     33                        } catch (IOException ioe) {
     34                                System.out .println("IO Exception in thread "+myName());
     35                        }
     36                }
     37                public static void main(String [] a) throws Exception {
     38                        numThreads = Integer .parseInt(a[0]);
     39                        datafile = a[1];
     40                        ST0[] st = new ST0[n];
     41                        for (int i=0; i<numThreads; i++) st[i] = new ST0(i);
     42                        for (int i=0; i<numThreads; i++) st[i].start();
     43                        for (int i=0; i<numThreads; i++) st[i].join();
     44                        System.out .println(" sum1 = "+sum1 + " sum2 = "+sum2);
     45                }
     46        }

This program , as we have seen, gets two arguments : the number of threads to create and the name of a data file consisting of integers , one per line. The program creates the requested number of threads to read and sum the data from the file using a simple parallel structure. Answer the questions below.

Explanation / Answer

a) br is a private member which is accessable only in same class in which it is decleared. Private members can not be use outside of its class.


b) Race condition in Java is a type of concurrency bug or issue which is introduced in your program because parallel execution of your program by multiple threads at same time


line 22, 23, 24

synchronized(datafile) {
sum1 += Integer .parseInt(line.trim());
}


and line 30, 31, 32

synchonized(synch2) {
sum2 += mysum;
}


c) by ommiting line 43. Multiple threads will not wait for joining together. If any thread complete its process then will start execute from 44 line.

while join() function will stop all threads till they join together then will start executing from next line


d) line 22, 23, 24 and line 30, 31, 32 are computing sum


Comutation of line 30, 31, 32 are more efficient because first sum (line 22, 23, 24) is calulating three funtional simultanously triming, parsing and addition. while second sum (line 30, 31, 32 is doing only addition. So second sum is much efficient.


e) Instance will not be created for all threads by ommiting line 41.


f) Because datafile variable is defined in a class and used in class method. If it would be defined in method itself and another variable with same name was passed in the method then for using local variable this keywork could be used.


g)if trimming and integar parsing functionality could be performed outside of synchronized block then efficiency can be increased.


h)It is checking the reminder of count devide by number of threads and comparing with myNum variable.