Because sleep is a static method, how can it possibly know what thread it needs to pause? Here is some code that synchronizes thread access to a shared variable. How come it is not guaranteed to...



Because sleep is a static method, how can it possibly know what thread it needs to pause?


Here is some code that synchronizes thread access to a shared variable. How


come it is not guaranteed to output 30,000 every time it is run?


public class Counter


{



private int counter;



public Counter()



{



counter = 0;



}


public int value()



{



return counter;



}



public synchronized void increment()



{



counter++;



}


}


public class RaceConditionTest extends Thread


{



private Counter countObject;



public RaceConditionTest(Counter ctr)



{



countObject = ctr;



}



public void run()



{



countObject.increment();



}



public static void main(String[] args)



{



int i;



Counter masterCounter = new Counter();



RaceConditionTest[] threads = new RaceConditionTest[30000];



System.out.println("The counter is " + masterCounter.


value());



for (i = 0; i <>



{



threads[i] = new RaceConditionTest(masterCounter);



threads[i].start();



}



System.out.println("The counter is " + masterCounter.


value());



}


}



May 19, 2022
SOLUTION.PDF

Get Answer To This Question

Submit New Assignment

Copy and Paste Your Assignment Here