Olá pessoal,
Alguem poderia detalhar o código abaixo não estou avaliando bem a execução dos Thread :shock:
1. class Waiting implements Runnable {
2. boolean flag = true;
3. public synchronized void run() {
4. if (flag) {
5. flag = false;
6. System.out.print("1 ");
7. try { this.wait(); } catch (Exception e) { }
8. System.out.print("2 ");
9. }
10. else {
11. flag = true;
12. System.out.print("3 ");
13. try { Thread.sleep(2000); } catch (Exception e) { }
14. System.out.print("4 ");
15. notify();
16. }
17. }
18. public static void main(String [] args) {
19. Waiting w = new Waiting();
20. new Thread(w).start();
21. new Thread(w).start();
22. }
23. }
Which two are true? (Choose two.)
A
The code outputs 1 3 4.
B
The code outputs 3 4 1.
C
The code outputs 1 2 3 4.
D
The code outputs 1 3 4 2.
E
The code never completes.
F
The code runs to completion.
Reference
Close
JLS 3.0,
Options D and F are correct.
