DO NOT USE SINGLE QUOTES OR DOUBLE QUOTES OR PARENTHESES IN YOUR ANSWERS! (a) Wh
ID: 3536501 • 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 andsum the data from the file using a simple parallel structure. Answer the questions below.
Explanation / Answer
ANSWERS
1=The C++ standard allows only static constant integral or enumeration types to be initialized inside the class. This is the reason a is allowed to be initialized while others are not.
If a static data member is of const integral or const enumeration type, its declaration in the class definition can specify a constant-initializer which shall be an integral constant expression (5.19). In that case, the member can appear in integral constant expressions. The member shall still be defined in a namespace scope if it is used in the program and the namespace scope definition shall not contain an initializer.
2=The important bit there is the 'has_called_status'. When that status is equal to zero, that means a person hasn't been called, and appears in the queue for calling.
My IVR has multiple types of calls it can make, so it adds people to different lists (see type_of_call). My issue is that I'm adding these lists in order, with the following query:
The reason you shouldn't leave off the php closing tag is because it causes an imbalance in the php tags and any programmer with half a mind can remember to not add extra white-space.
So for your question:
Is there another good reason to skip the ending php tag?
No, there isn't another good reason to skip the ending php tags.
I will finish with some arguments for not bothering with the closing tag:
People are always able to make mistakes, no matter how smart they are. Adhering to a practice that reduces the number of possible mistakes is (IMHO) a good idea.
PHP is not XML. PHP doesn't need to adhere to XMLs strict standards to be well written and functional. If a missing closing tag annoys you, you're allowed to use a closing tag, it's not a set-in-stone rule one way or the other.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.