Oracle
1Z0-809 · Question #257
Given: ``java 1. class MyClass implements Runnable { 2. public int value; 3. public void run() { 4. while (value < 100) { 5. value++; 6. System.out.println("value: " + value); 7. } 8. } 9. } 10…
The correct answer is A. Line 3: public synchronized void run() {. You've hit your session limit · resets 5:20pm (America/New_York)
Question
Given:
1. class MyClass implements Runnable {
2. public int value;
3. public void run() {
4. while (value < 100) {
5. value++;
6. System.out.println("value: " + value);
7. }
8. }
9. }
10. public class TestThread {
11. public static void main(String[] args) {
12. MyClass mc = new MyClass();
13. Thread a = new Thread(mc);
14. a.start();
15. Thread b = new Thread(mc);
16. b.start();
17. }
18. }
What change should you make to guarantee a single order of execution (printed values 1 -100 in order)?Options
- ALine 3: public synchronized void run() {
- BLine 1: class MyClass extends Thread {
- CLine 2: public volatile int value;
- DLine 2: public synchronized int value;
How the community answered
(56 responses)- A71% (40)
- B7% (4)
- C4% (2)
- D18% (10)
Explanation
You've hit your session limit · resets 5:20pm (America/New_York)
Community Discussion
No community discussion yet for this question.