oi pessoal
estou com uma dúvida em uma questão de thread (achei um topico sobre o mesmo assunto mas não consegui entender http://www.guj.com.br/java/105366-duvida-questao-thread---testkiller)
public class NameList {
private List names = new ArrayList();
public synchronized void add(String name) {
names.add(name);
}
public synchronized void printAll() {
for (int i = 0; i < names.size(); i++) {
System.out.print(names.get(i) + "");
}
}
public static void main(String[] args) {
final NameList sl = new NameList();
for (int i = 0; i < 2; i++) {
new Thread() {
public void run() {
sl.add("A");
sl.add("B");
sl.add("C");
sl.printAll();
}
}.start();
}
}
}
A. An exception may be thrown at runtime.
B. The code may run with no output, without exiting.
C. The code may run with no output, exiting normally.
D. The code may rum with output ?A B A B C C ?, then exit.
E. The code may rum with output ?A B C A B C A B C ?, then exit.
F. The code may ruin with output ?A A A B C A B C C ?, then exit.
G. The code may ruin with output ?A B C A A B C A B C ?, then exit.
Answer: EG
a letra E eu entendi, foi a saida que deu, mas não entendi a G… alguem pode me explicar?
abraço
