1

Question II This question carries 50% of the marks for this assignment. This question is about...

Question

Question II This question carries 50% of the marks for this assignment. This question is about...

Question II

This question carries 50% of the marks for this assignment.

This question is about avoiding race condition by using synchronization mechanism provided by the JAVA programming language. We provided you with a Netbeans JAVA project. The project idea is as follows: we want to merge the content of four files into a single file, as follows. Below is the content of all four files:

F1 content: 1 1 1 1

F2 content: 2 2 2 2

F3 content: 3 3 3 3

F4 content: 4 4 4 4

After merging them into the Sink file, we expect the following content in the sink file:1234123412341234

We want to read from each file only one integer value at a time (starting form file number 1, number 2 and so on) and write theses integers values ( in the same order we read them) into a specific sink file, we want to repeat it until all file files are empty.

Please note that all files have the same number of integers.

We have provided you with a template project that needs to be modified to accomplish the above task correctly.

The project is composed of the following classes including four text files:

Four text files called “f1.txt”, “f2.txt”, “f3.txt” and “f4.txt”, each file has a sequence of integer corresponding its name, for instance “f2.txt” contains a sequence of integers all are 2.

The following JAVA source code files are included in the NetBeans projects:

WriteToFile: A helper class to write into a file.

SyncWriteToFile: which control the access to the file, it does so by aggregating an object of the class “WriteToFile”

MergeFiles that extends Thread: it is intended to read all integers form a specific file and use an object of “SyncWriteToFile” to write them into the sink file at the appropriate place.

TMA_Tm298_SummerEgJo: contains the main method to test the program

Read the source code of the above classes carefully and run the project. You will discover that the content of the sink file is not as expected and each time you run it you got a different content. Provide screen shots form running the project without any modification. Provide convincing justification, why you got these results. [15 marks]

In order to get the expected sink file content, you need to modify the project as follows:

You can do so by first identifying the shared resources and then the critical section code in the project. [10 marks]. Then you need to change the critical section code so that the sink file contains the following result no matter how many times you run the project. The sink file correct content is :1234123412341234. [25 marks]

You are sked to submit the whole project (as one zip file) after all modification.

Answers

// PLEASE LIKE THE SOLUTION
// FEEL FREE TO DISCUSSION IN COMMENT SECTION
// JAVA PROGRAM

import java.io.*;
import java.util.*;

class WriteToFiles{
   // attribute to store file writer
   public static void writeFile(FileWriter myWriter,char a){
       try{
           myWriter.write(a);
       }
       catch(Exception e){
           System.out.println(e.getMessage());
       }
   }
}

class MergeFiles extends Thread{
   // run method
   // attribute
   // writer file
   static FileWriter myWriter;
  
   // reader file
   static FileReader reader;

   public void run(){
       try{
           // read and write to file
           int i;
           if((i = reader.read()) != -1){
               WriteToFiles wr = new WriteToFiles();
               wr.writeFile(myWriter,(char)i);
               Test.done = 0;
           }
           else{
               Test.done++;
           }
       }
       catch (Exception e){
           // message
           System.out.println ("Exception is caught");
       }
   }
}

// Test
class Test{
   // attribute
   static int done = 0;

   public static void main(String rg[]){

       try{

           MergeFiles.myWriter = new FileWriter(new File("output.txt"));
           FileReader file1 = new FileReader(new File("f1.txt"));
           FileReader file2 = new FileReader(new File("f2.txt"));
           FileReader file3 = new FileReader(new File("f3.txt"));
           FileReader file4 = new FileReader(new File("f4.txt"));

           // now call reader writer in sync way
           while(Test.done != 4){

               MergeFiles.reader = file1;
               MergeFiles r1 = new MergeFiles();
               r1.run();
               r1.join();

               MergeFiles.reader = file2;
               MergeFiles r2 = new MergeFiles();
               r2.run();
               r2.join();

               MergeFiles.reader = file3;
               MergeFiles r3 = new MergeFiles();
               r3.run();
               r3.join();

               MergeFiles.reader = file4;
               MergeFiles r4 = new MergeFiles();
               r4.run();
               r4.join();
           }

           // now close files
           MergeFiles.myWriter.close();
       }
       catch(Exception e){
           System.out.println(e.getMessage());
       }
   }

}

// SAMPLE OUTPUT

f1.txt x 1111

F2.txt x 2222

f3.txt 3333

f4.txt 4444

output.txt x Syn 1234123412341234


Similar Solved Questions

1 answers
Required information [The following information applies to the questions displayed below.] Preble Company manufactures one product....
Required information [The following information applies to the questions displayed below.] Preble Company manufactures one product. Its variable manufacturing overhead is applied to production based on direct labor-hours and its standard cost card per unit is as follows: Direct material: 5 po...
1 answers
WHMWK 12 Due Thursday by 1pm Allowed Attempts 1 Points 10 Submitting a file upload Fi...
WHMWK 12 Due Thursday by 1pm Allowed Attempts 1 Points 10 Submitting a file upload Fi Available Jul 29 at 12am - Jul 30 at 11:59pm 2 day Q1) Let A = 2 0 1 3 2 2 0 -1 0 a) Determine all eigenvalues of A. b) Determine the basis for each eigenspace of A c) Determine the algebraic and geometric multipli...
2 answers
Suppose the correlation coefficient between the rates of return on ABC Mutual Fund and the market...
Suppose the correlation coefficient between the rates of return on ABC Mutual Fund and the market portfolio is 0.6. The standard deviations of the rates return are 0.30 for ABC and 0.20 for the market portfolio. How would you combine the fund and the riskless asset to obtain a portfolio with a relat...
1 answers
Private placements refer to: indirect negotiation with the one or more creditors. debt or equity securities...
Private placements refer to: indirect negotiation with the one or more creditors. debt or equity securities issued to individuals or organizations without being listed with a securities commission. shares that form part of a control block. funds not disclosed by private companies....
1 answers
Question 9 of 9 < - /3.8 = View Policies Current Attempt in Progress Cheyenne Inc....
Question 9 of 9 < - /3.8 = View Policies Current Attempt in Progress Cheyenne Inc. went public 3 years ago. The board of directors will be meeting shortly after the end of the year to decide on a dividend policy. In the past, growth has been financed primarily through the retention of earnings. A...
1 answers
How influential is the field of physics on astronomy?
How influential is the field of physics on astronomy?...
1 answers
Critical Values for Correlation Coefficient n 3 0.997 4 0.950 5 0.878 6 0.811 7 0.754...
Critical Values for Correlation Coefficient n 3 0.997 4 0.950 5 0.878 6 0.811 7 0.754 8 0.707 9 0.666 10 0.632 11 0.602 12 0.576 13 0.553 14 0.532 15 0.514 16 0.497 17 0.482 18 0.468 19 0.456 ...
1 answers
How do you solve #-5x^2-x-8=0# using the quadratic formula?
How do you solve #-5x^2-x-8=0# using the quadratic formula?...
1 answers
A quarry crane is used to lift massive rocks from a quarry pit. Consider the simplified...
A quarry crane is used to lift massive rocks from a quarry pit. Consider the simplified model of such a crane shown in the figure. (Figure 1) The ends of two poles are anchored to the ground at the same point (point O). From this point, one pole rises vertically and the second pole rises at an angle...
1 answers
Problem 10-16A (Algo) Using present value techniques to evaluate alternative investment opportunities LO 10-2 Stuart Delivery...
Problem 10-16A (Algo) Using present value techniques to evaluate alternative investment opportunities LO 10-2 Stuart Delivery is a small company that transports business packages between New York and Chicago. It operates a fleet of small vans that moves packages to and from a central depot within ea...
1 answers
No calculator please 4. (12 pts) Show the matrix operator T: RR given by the following...
no calculator please 4. (12 pts) Show the matrix operator T: RR given by the following equations is one-to-one; Find the standard matrix for the inverse operator T-I, and find T-(W1, W2, w3). w1 = x +22-23 w2 = 2.v 1 + 2x2 - 23 W3 = x - 2.12...
1 answers
25. Carbon dioxide can be formed using two different isotopes of carbon: 12CO2 and 1"CO2. a....
25. Carbon dioxide can be formed using two different isotopes of carbon: 12CO2 and 1"CO2. a. Calculate the relative rates of diffusion for both 12CO2 and 1CO2. Which gas diffuses faster and why? b....
1 answers
A neutral water molecule (H_2O) in its vapor state has an electric dipole moment of magnitude...
A neutral water molecule (H_2O) in its vapor state has an electric dipole moment of magnitude 6.2 times 10^-30 cm. How far apart are the molecule's center of positive and negative charge? (A) 1.9 m (B) 2.9 pm (C) 3.9 pm (D) 4.9 pm (E) 5.9 pm...
1 answers
Write two balanced equations: (step 1) reaction of benzil with NaBH4 to form a “dialkoxide” and...
Write two balanced equations: (step 1) reaction of benzil with NaBH4 to form a “dialkoxide” and (step 2) reaction of the formed “dialkoxide” with water to produce hydrobenzoin. NaBH4 is the source of four equivalents of the hydride ion, H-...
1 answers
3. The following polarographic diffusion currents were measured at -0.6 V for Cuso, in 2 M...
3. The following polarographic diffusion currents were measured at -0.6 V for Cuso, in 2 M NH CI/NH,. Prepare an excel calibration plot and use it to estimate the molarity of an unknown solution giving Id = 15.6 LA. [Cu (mm) 0.0393 0.0780 0.1585 0.489 0.9901.97 3.83 8.43 L (MA) 0.256 0,520 10.58 3.0...
1 answers
You are considering a project with an initial cash outlay of $75,000 and expected free cash...
You are considering a project with an initial cash outlay of $75,000 and expected free cash flows of $22,000 at the end of each year for 7 years. The required rate of return for this project is 9 percent. a. What is the​ project's payback​ period? b. What is the​ project's ...
1 answers
Problem #1: TO SELECT THE MOST ECONOMICAL Wio SHAPE COLUMN ZO FEET IN HEIGHT SUPPORT AH...
Problem #1: TO SELECT THE MOST ECONOMICAL Wio SHAPE COLUMN ZO FEET IN HEIGHT SUPPORT AH AXIAL LORD OF 370 KIPS using soksi STEEL! ASSUME A FIXED BASE ANDA PINGED TOP (CASE C) WIDE FLANGE SHAPES HP Axis Y-Y Theoretical Dimensions and Properties for Designing Flange Axis X-X | Weight Area Depth Web Se...
1 answers
SI Worksheet Week 7 • Compare the functions of photosystems I and II. • Why do...
SI Worksheet Week 7 • Compare the functions of photosystems I and II. • Why do we need oxygen (02) to live? • The Calvin cycle reactions are something called the "dark reactions" to contrast them with the light reactions. Can the Calvin cycle be sustained in a plant that is ke...
1 answers
)The thaxel tbeuen an arpat and a cents Giłis sampad for Public bises and Pmieke tasvat's....
)The thaxel tbeuen an arpat and a cents Giłis sampad for Public bises and Pmieke tasvat's. A sampe of cigkt bs tups yel to 4,5, 31,42 Ton fou (abs re included in tk Survey uwith times or 37, ч6/ 40, 327 35/46/33 41 24,3C n iven te trax) tedffcne, can we concide thet te tax cabs get te...

-- 0.010401--