Questao de Threads

3 respostas
caiozanchetti

Galera, segue uma questao que quero trocar uma ideia com vcs:
veleu…

Givem:

1. class Work implements Runnable {
 2.   Thread other;
 3.   Work(Thread other) { this.other = other; }
 4.   public void run() {
 5.     try { other.join(); } catch (Exception e) { }
 6.     System.out.print("after join ");
 7. } }
 8.
 9. class Launch {
10.   public static void main(String [] args) {
11.     new Thread(new Work(Thread.currentThread())).start();
12.     System.out.print("after start ");
13. } }

What is the result?
A) after join
B) after start
C) Compilation fails.
D) after join after start
E) after start after join
F) An exception is thrown at runtime.

3 Respostas

sergiolpf

acredito q a resposta seja letra “d”.
O join diz pro scheduler (acho q é assim q escreve) que a instancia que está chamando o join deve esperar até que a thread encapsuladora complete. Nesse caso ele diz q a thread other (main) deve esperar até a thread anonima (será isso??) complete o seu run.
Acho q é isso. Se eu estiver errado por favor me corrijam estou estudando pra cert tambem =o)

dkotvan

Acho que a resposta certa é a “E”.

Na linha 5 ele chama o método join na thread principal. Este método faz com que a thread corrente aguarde pela thread principal terminar primeiro.
Ele irá aguardar a thread principal terminar e depois irá continuar.

Está no javadoc -> http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html#join()

sergiolpf

valeu. =o)

Criado 20 de maio de 2006
Ultima resposta 22 de mai. de 2006
Respostas 3
Participantes 3