Pessoal , estou com uma dúvida na questão 15 do capítulo 9(pág 424) do livro da kathy.
O trecho de código é este :
public class ThreadDemo {
synchronized void a() { actBusy(); }
static synchronized void b() { actBusy(); }
static void actBusy(){
try
{
Thread.sleep(1000);
} catch (InterruptedException e) {}
}
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.b(); break;
}
}
};
Thread thread1 = new Thread (runnable);
Thread thread2 = new Thread (runnable);
thread1.start();
thread2.start();
}
}
x.a() em thread1 e x.b() em thread2 pode acontecer.
x.a() em thread1 e y.b() em thread2 pode acontecer.
x.b() em thread1 e x.a() em thread2 pode acontecer.
x.b() em thread1 e y.b() em thread2 pode acontecer.
Porque essas opções estão corretas se os métodos estáticos bloqueiam a classe ? (Entendo que se bloqueia a classe , nenhuma instância conseguirá acesso …) Não entendi esta questão …