questão sobre threads

1 resposta
A

What will happen when you attempt to compile and run the following code?

public class BriarMill extends Thread{
    public static void main(String argv[]){
        BriarMill bm = new BriarMill();
        bm.go();
    }
    public void go(){
        start();
    }
    public  void payBill(){
        try{
            wait();
        }catch(InterruptedException ie){}
        System.out.println("done");
    }
    public void run(){
        payBill();
        notifyAll();
    }
}

1 - Compilation but runtime error.
2 - Compilation and output of done at runtime
3 - Compilation but no output at runtime, the program will run and wait with no output.
4 - Compile time error

1 Resposta

E

Acho que é o 1…o método payBill não é synchronized e está tentando dar wait (vai lançar uma exceção com mensagem “Current thread not owner”).

Para chamar wait, notify ou notifyAll, um método precisa ser synchronized ou a chamada tem que estar dentro de um bloco synchronized.

Criado 12 de abril de 2004
Ultima resposta 13 de abr. de 2004
Respostas 1
Participantes 2