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()); } }
Información del documento
- Subido en
- 7 de febrero de 2023
- Número de páginas
- 6
- Escrito en
- 2022/2023
- Tipo
- Examen
- Contiene
- Preguntas y respuestas