92 Program with synchronized and non-synchronized thread in Java

Code :

class Institute 
{
synchronized public 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:



Previous
Next Post »