class SynchDemo
{
void printNumber(int n)
{
synchronized(this)
{
for(int i=1;i<=5;i++)
{
System.out.println(n+" * "+i+" = "+n*i);
try
{
Thread.sleep(400);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
}
class MainThread1 extends Thread
{
SynchDemo t;
MainThread1(SynchDemo t)
{
this.t=t;
}
public void run()
{
t.printNumber(4);
}
}
class MainThread2 extends Thread
{
SynchDemo t;
MainThread2(SynchDemo t)
{
this.t=t;
}
public void run()
{
t.printNumber(10);
}
}
public class TestSynchronizedBlock
{
public static void main(String args[])
{
SynchDemo obj = new SynchDemo();
MainThread1 t1=new MainThread1(obj);
MainThread2 t2=new MainThread2(obj);
t1.start();
t2.start();
}
}