Question: Implement a Java program that creates two new threads that are in a race condi - tion. Use the problem from slide 6 - 6

Implement a Java program that creates two new threads that are in a race condi- tion. Use the problem from slide 6-6 and 6-7 as example, however you do not use a spooler directory, i.e. just increment a shared variable in by copying it into a variable next_free_slot that is local to each thread (comparable to a CPU register in fact the Java compiler/virtual machine likely use a CPU register for that local variable), incrementing that variable by 1, and writing it back to in.
in shall be initialised with 0. Each of the two threads shall increment the in in a loop using the same number of iterations (the number is passed in as command line parameter) and terminate after that number of incrementations. Use the join() method to wait for the termination of the two threads. After that, the value of in is printed out.
As starting point, use the files from tol401g_assignment11.zip that you find in Canvas
File Assignment11.java contains the main method that reads the number of iter- ations to be executed by each thread and prints the result out do not change that file!
Use file MyAssignment11.java to add your implementation there (it gets called by file Assignment11.java). Feel free to change that file except:
(a) Do not change the name of this class (adding extends is OK) nor put it into another package.
(b) Do not modify the name and input and output parameter of the main method.
You are allowed to add further classes.
Hints:
As each thread is executed by (or within) two different objects (created by you via new), it is not sufficient to define in as ordinary field (outside of Java, fields are known as attributes) within the two thread objects. The object-oriented concept of encapsulation requires to define the shared variable either as static so that it is shared by all instances of that class or to introduce a separate class (e.g. named Counter with, e.g., a method increment()) or a public in variable which are instantiated by you and passed to both thread instances.
Depending on how threads are scheduled on your machine, the data type int may not be large enough to provoke a race condition, hence you better use the data type long for your counters and number of iterations variables.
You need to define your variable that is shared by the threads as volatile (see slide 6-41), e.g. volatile long in.2. Run your program: is the printed value of in the expected value? How would you see that a race condition occurred?
3. What command line parameter value demonstrates typically at each run the race con- dition on your computer? What is the name and version of your OS (e.g.MS Windows 10)?// You are free to modify this class, e.g. add fields, methods,
// constructors, extend from class Thread etc. with the two exceptions:
//1. Do not change the name of this class nor put it into another package
//2. Do not modify the name and input and output parameter of the main method.
public class MyAssignment11{// TODO: Modify as necessary
// TODO: fill in your code here
public static long main(long iterationsPerThread){// Do not modify this line!
// TODO: fill in your code here
}
}
// Any modifications to this file will be ignored.
public class Assignment11{
public static void main(String[] args){
if (args.length 1){
System.out.println("One command line parameter only: must be an interger (or long) number");
} else {
long iterationsPerThread = Long.parseLong(args[0]);
// Call to student's class:
// takes number of iterations per thread as input
// returns result of two threads each incrementing a shared
// variable "in"(initialised with 0) "iterationsPerThread" times.
long result = MyAssignment11.main(iterationsPerThread);
System.out.println(result);
}
}
}
 Implement a Java program that creates two new threads that are

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!