Boa tarde, pessoal.
Uma questão no testkiller da seguinte e pergunta o que pode ser impresso:
class Computation extends Thread {
private int num;
private boolean isComplete;
private int result;
public Computation(int num) { this.num = num; }
public synchronized void run() {
result = num * 2;
isComplete = true;
notify();
}
public synchronized int getResult() {
while (!isComplete) {
try {
wait();
} catch (InterruptedException e) { }
}
return result;
}
public static void main(String[] args) {
Computation[] computations = new Computation [4];
for (int i = 0; i < computations.length; i++) {
computations[i] = new Computation(i);
computations[i] .start();//Aqui irão ter muitas threads disputando o contexto syncronizado qual grantias de qual irá vencer
}
for (Computation c : computations)
System.out.print(c.getResult() +" ");
}
}
E dá as seguintes opções:
A. The code will deadlock.
B. The code may run with no output.
C. An exception is thrown at runtime.
D. The code may run with output ?0 6?.
E. The code may run with output ?2 0 6 4?.
F. The code may ruin with output ?0 2 4 6?.
A resposta certa é a F, eu testei aqui e só deu este resultado,gostaria de saber pq não possi vel ter uma saída como
a opção E?
Agradeço as respostas.