public class TestThr
{
public static void main(
String[] a)
{
MyTH mt=new MyTH();
Thread t1=new Thread(mt);
Thread t2=new Thread(mt);
t1.setName("1st thread");
t2.setName("2nd thread");
t1.start();
t2.start();
}
}
class MyTH implements Runnable
{
public void run()
{
mt1();
}
//synchronized method
synchronized private void mt1()
{
System.out.println("synchronized method is executed
by"+Thread.currentThread().getName());
mt2();
}
//non-synchronized method
private void mt2()
{
System.out.println("non-synchronized method is executed
by"+Thread.currentThread().getName());
}
}
output is:
synchronized method is executed by 1st thread
non-synchronized method is executed by 1st thread
synchronized method is executed by 2nd thread
non-synchronized method is executed by 2nd thread
or
synchronized method is executed by 2nd thread
non-synchronized method is executed by 2nd thread
synchronized method is executed by 1st thread
non-synchronized method is executed by 1st thread
but here 2nd thread is not accessing the non-synchronized method when synchronized ,method is accessed by 1st thread.....
can you give me explanation regarding this?