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 and as example, however you do not use a spooler directory, ie just increment a shared variable in by copying it into a variable nextfreeslot that is local to each thread comparable to a CPU register in fact the Java compilervirtual machine likely use a CPU register for that local variable incrementing that variable by and writing it back to in
in shall be initialised with 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 tolgassignmentzip that you find in Canvas
File Assignmentjava 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 MyAssignmentjava to add your implementation there it gets called by file Assignmentjava 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 objectoriented 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 eg named Counter with, eg 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 eg volatile long in Run your program: is the printed value of in the expected value? How would you see that a race condition occurred?
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 egMS Windows You are free to modify this class, eg add fields, methods,
constructors, extend from class Thread etc. with the two exceptions:
Do not change the name of this class nor put it into another package
Do not modify the name and input and output parameter of the main method.
public class MyAssignment TODO: Modify as necessary
TODO: fill in your code here
public static long mainlong iterationsPerThread Do not modify this line!
TODO: fill in your code here
Any modifications to this file will be ignored.
public class Assignment
public static void mainString args
if argslength
System.out.printlnOne command line parameter only: must be an interger or long number";
else
long iterationsPerThread Long.parseLongargs;
Call to student's class:
takes number of iterations per thread as input
returns result of two threads each incrementing a shared
variable ininitialised with "iterationsPerThread" times.
long result MyAssignmentmainiterationsPerThread;
System.out.printlnresult;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
