Threads

Alguem poderia me ajudar? Threads ainda está confuso…

Given: public class Starter extends Thread { private int x= 2; public static void main(String[] args) throws Exception { new Starter().makeItSo(); } public Starter() { x=5; start(); } public void makeItSo() throws Exception { join(); x=x- 1; System.out.println(x); } public void run() { x *= 2; } } What is the output if the main() method is rum? A. 4 B. 5 C. 8 D. 9 E. Compilation fails. F. An exception is thrown at runtime. G. It is impossible to determine for certain. Answer: D

Simples, ele inicia a thread no construtor, x tem o valor de 5, a thread multiplica o valor de x por 2, agora x vale 10, o método makeItSo faz uma chamada para o método join (que vai se unir ao final da execução de uma outra thread) e após isso subtrai 1 de x, agora x possui o valor 9

public class Starter extends Thread { private int x= 2; public static void main(String[] args) throws Exception { new Starter().makeItSo(); } public Starter() { x=5; start(); } public void makeItSo() throws Exception { join(); x=x- 1; System.out.println(x); } public void run() { x *= 2; } }

new Starter().makeItSo();

Ele vai en Starter()
X agora e 5;

da um START this.START();
Que vai para o run

no run ele faz
x *= 2 (mesma coisa que x = x *2;)
x agora é 10
depois ele sai do newstarter e vai pro .makeitso();
Lá ele tem um join que faz ele ser o atual executando…

X-1 e mostra… 9