Question: Read given code RaceOrNot 1 . java and write all possible outputs of the program when it is run as java RaceOrNot 1 3 .

Read given code RaceOrNot1.java and write all possible outputs of the program when it is run as java RaceOrNot13. If you believe there is only one possible output, you just need to write that output.
Code:
//Can you rewrite this program into an equivalent program using Lock or Semaphore instead of using synchronized?
public class RaceOrNot1 implements Runnable {
static int num_threads;
static int []sa={0,0};
int index;
RaceOrNot1(int x){
index=x;
}
public void run(){//implement the method run in Runnable interface
synchronized(this){
for(int i=0;i100000000;i++){
sa[index%2]=(sa[index%2]+1)%2;
}//for
}
}//run
static void printSa(){
System.out.println(String.valueOf(sa[0])+""+String.valueOf(sa[1]));
}//printSa
public static void main(String [] args){
if(args.length!=1){
System.err.println("need one command line argument n.");
System.exit(0);
}
num_threads=Integer.parseInt(args[0]);//number of threads that the user inputs
if(num_threads2|| num_threads>6){
System.err.println("number of threads should be between 2 and 6.");
System.exit(0);
}
RaceOrNot1 at[]=new RaceOrNot1[num_threads];//create array to hold task objects
Thread arrT []= new Thread[num_threads];//create array to hold Thread objects
for(int i=0;i
Read given code RaceOrNot 1 . java and write all

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 Programming Questions!