public class ThreadDemo {
synchronized void a(){
actBusy();
}
static synchronized void b() {
actBusy();
}
static void actBusy(){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO: handle exception
}
}
public static void main(String[] args) {
final ThreadDemo x = new ThreadDemo();
final ThreadDemo y = new ThreadDemo();
Runnable runnable = new Runnable(){
public void run(){
int option = (int) (Math.random() * 4);
switch(option){
case 0: x.a(); break;
case 1: x.b(); break;
case 2: y.a(); break;
case 3: y.a(); break;
}
}
};
Thread thread1 = new Thread(runnable);
Thread thread2 = new Thread(runnable);
thread1.start();
thread2.start();
}
}
/*
[b]Quais dos seguintes pares de chmadas a métodos nunca poderiam ser executados ao mesmo tempo?[/b]
a) x.a() em thread1, e x.a() em thread2
b) x.a() em thread1, e x.b() em thread2
c) x.a() em thread1, e y.a() em thread2
d) x.a() em thread1, e y.b() em thread2
e) x.b() em thread1, e x.a() em thread2
f) x.b() em thread1, e x.b() em thread2
g) x.b() em thread1, e y.a() em thread2
h) x.b() em thread1, e y.b() em thread2
[i]RESPOSTA:[/i][u]
[b]A,F e H[/b]
*/
Galéra eu não entendi o resultado, será que pode ser possível uma explicação de alguêm que saiba ou que também está estudando?
Desde já agradeço.

