class SynchronizationDemo
{
synchronized void printTable(int n,String s2) //synchronized method synchronized {
System.out.println(s2);
for(int i=1;i<=5;i++)
{
System.out.println(n*i);
try {
Thread.sleep(400);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
class MyThread1 extends Thread
{
Table t;
String s1="Table of five";
MyThread1(Table t)
{
this.t=t;
}
public void run()
{
t.printTable(7,s1);
}
}
class MyThread2 extends Thread
{
Table t;
String s1="Table of Ten";
MyThread2(Table t)
{
this.t=t;
}
29 public void run()
{
t.printTable(9,s1);
}
}
public class SynchronizationDemo
{
public static void main(String args[])
{
Table obj = new Table();//only one object MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
t1.start();
t2.start();
}
}
**OUTPUT***
D:\TY\Adv Java>javac SynchronizationDemo.java
D:\TY\Adv Java>java SynchronizationDemo
Table of five
7 14 21 28 35 Table of Ten
9 18 27 36 45