public class ThreadDemo
{
synchronized void a()
{
actBusy();
}
static synchronized void b()
{
actBusy();
}
static void actBusy()
{
try
{
System.out.println(Thread.currentThread().getName());
Thread.sleep(1000);
}
catch(Exception 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 t1 =new Thread(runnable,"One");
Thread t2 =new Thread(runnable,"Two");
t1.start();
t2.start();
}
}
The answer here is One
Two.
Can someone please explain me how this program works.I am getting confused with the static synchronized, non-static synchronized methods.