Exam (elaborations) java programming(Java Never Ends)
1. Because sleep is a static method, how can it possibly know what thread it needs to pause? 4. 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() { countOment(); } public static void main(String[] args) { int i; Counter masterCounter = new Counter(); RaceConditionTest[] threads = new RaceConditionTest[30000]; Sln("The counter is " + masterCounter. value()); for (i = 0; i < h; i++) { threads[i] = new RaceConditionTest(masterCounter); threads[i].start(); } Sln("The counter is " + masterCounter. value()); } }
Written for
- Institution
-
Harvard University
- Course
-
Java programming
Document information
- Uploaded on
- February 7, 2023
- Number of pages
- 6
- Written in
- 2022/2023
- Type
- Exam (elaborations)
- Contains
- Questions & answers
Subjects
Also available in package deal