class Table1
{
synchronized void printTable1(int n)
{
synchronized(this)
{
for(int i=1;i<=5;i++)
{
System.out.println(n*i);
try
{
Thread.sleep(500);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
}
class MyThread3 extends Thread
{
Table1 t;
MyThread3(Table1 t)
{
this.t=t;
}
public void run()
{
t.printTable1(5);
}
}
class MyThread4 extends Thread
{
Table1 t;
MyThread4(Table1 t)
{
this.t=t;
}
public void run()
{
t.printTable1(100);
}
}
class Use1
{
public static void main(String surat[])
{
Table1 obj=new Table1();
MyThread3 t1=new MyThread3(obj);
MyThread4 t2=new MyThread4(obj);
t1.start();
t2.start();
}
}
Output:
{
synchronized void printTable1(int n)
{
synchronized(this)
{
for(int i=1;i<=5;i++)
{
System.out.println(n*i);
try
{
Thread.sleep(500);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
}
class MyThread3 extends Thread
{
Table1 t;
MyThread3(Table1 t)
{
this.t=t;
}
public void run()
{
t.printTable1(5);
}
}
class MyThread4 extends Thread
{
Table1 t;
MyThread4(Table1 t)
{
this.t=t;
}
public void run()
{
t.printTable1(100);
}
}
class Use1
{
public static void main(String surat[])
{
Table1 obj=new Table1();
MyThread3 t1=new MyThread3(obj);
MyThread4 t2=new MyThread4(obj);
t1.start();
t2.start();
}
}
Output:
No comments:
Post a Comment