Program on if static synchronized method is executing then other static synchronized method won't executing but static non synchronized method , non-static synchronized ,non-static non synchronized method are executing.
Output:
Code :
class Institute
{
synchronized public static void classRoom( String facultyName )
{
for(int i=1;i<=10;i++)
{
System.out.println(i+". Class Taking By: "+facultyName);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
public void exam()
{
for(int i=1;i<=10;i++)
{
System.out.println(i+". Exam: "+Thread.currentThread().getName());
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
class MyThread extends Thread
{
Institute ins;
String facultyName;
@Override
public void run()
{
ins.classRoom(facultyName);
}
MyThread( Institute ins , String facultyName )
{
this.ins = ins;
this.facultyName = facultyName;
}
}
public class Test extends Thread
{
public static void main(String args[]) throws InterruptedException
{
Institute ins = new Institute();
MyThread mt1 = new MyThread(ins , "atharva");
MyThread mt2 = new MyThread(ins , "ramchandra");
mt1.start();
mt2.start();
ins.exam();
}
}
{
synchronized public static void classRoom( String facultyName )
{
for(int i=1;i<=10;i++)
{
System.out.println(i+". Class Taking By: "+facultyName);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
public void exam()
{
for(int i=1;i<=10;i++)
{
System.out.println(i+". Exam: "+Thread.currentThread().getName());
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
class MyThread extends Thread
{
Institute ins;
String facultyName;
@Override
public void run()
{
ins.classRoom(facultyName);
}
MyThread( Institute ins , String facultyName )
{
this.ins = ins;
this.facultyName = facultyName;
}
}
public class Test extends Thread
{
public static void main(String args[]) throws InterruptedException
{
Institute ins = new Institute();
MyThread mt1 = new MyThread(ins , "atharva");
MyThread mt2 = new MyThread(ins , "ramchandra");
mt1.start();
mt2.start();
ins.exam();
}
}
Output: