Oracle
1Z0-851 · Question #220
Given: public class TestSeven extends Thread { private static int x; public synchronized void doThings() { int current = x; current++; x = current; } public void run() { doThings(); } } Which…
The correct answer is E. Declaring the doThings() method as static would make the class thread-safe. See the full explanation below for the reasoning.
Question
Given: public class TestSeven extends Thread { private static int x; public synchronized void doThings() { int current = x; current++; x = current; } public void run() { doThings(); } } Which statement is true?
Options
- ACompilation fails.
- BAn exception is thrown at runtime.
- CSynchronizing the run() method would make the class thread-safe.
- DThe data in variable "x" are protected from concurrent access problems.
- EDeclaring the doThings() method as static would make the class thread-safe.
- FWrapping the statements within doThings() in a synchronized(new Object()) { } block would make
How the community answered
(38 responses)- A3% (1)
- B3% (1)
- D11% (4)
- E76% (29)
- F8% (3)
Community Discussion
No community discussion yet for this question.