java synchronized(this)模块中this参数的含义。

定义了如下3个类,大致情况的主进程同时启动两个test2和test3线程。
test2和test3的run方法内部都添加了synchronized模块,当两个模块中的参数互指或互不指,即test2种模块参数指向test3同时test3的参数指向test2,或test2和test3的参数都指向自己this时,运行结果为2个test2,test3线程互相等待但test2和test3线程同时运行:
new test()
t2 0
t3 0
t3 1
t2 1
t3 2
t2 2
t2 3
t3 3
t3 4
t2 4
t3 5
t2 5
t2 0
t3 6
t2 1
t3 7
t3 8
t2 2
t2 3
t3 9
t2 4
t3 10
t2 5
t3 0
t3 1
t3 2
t3 3
t3 4
t3 5
t3 6
t3 7
t3 8
t3 9
t3 10

当test2的参数指向test3,test3的参数指向自己this;或者test2的参数指自己,test3的参数指test2时,运行结果是4个test2,test3线程互相等待,同时只有一个线程运行:
new test()
t2 0
t2 1
t2 2
t2 3
t2 4
t2 5
t3 0
t3 1
t3 2
t3 3
t3 4
t3 5
t3 6
t3 7
t3 8
t3 9
t3 10
t2 0
t2 1
t2 2
t2 3
t2 4
t2 5
t3 0
t3 1
t3 2
t3 3
t3 4
t3 5
t3 6
t3 7
t3 8
t3 9
t3 10

并且不论synchronized块的参数是什么,同一个对象的运行是被锁住了,即两个test2和两个test3之间只能同时运行一个。
想问下高手jvm如何产生这个特性的原理是什么,这个参数又有什么方面的应用啊?

public class test implements Runnable{

Thread t;
Thread t1;
test2 t2=new test2() ;
test3 t3=new test3();
Thread T3;
Thread TT3;

test()
{

    System.out.println("new test()");
    t=new Thread(this);
    t1=new Thread(this);

    t2.t3=t3;
    t3.t2=t2;
    t.start();
    t1.start();
    T3=new Thread(t3);
    TT3=new Thread(t3);
    T3.start();
    TT3.start();


}

public void run()
{   

    t2.speak();
}




public static void main(String[] args) 
{
    // TODO Auto-generated method stub

new test( );

}

}

public class test2
{

test3 t3;

public void speak()
{

synchronized(t3){   //test2中参数指向test3线程

for(int i=0;i<=5;i++)
{


    System.out.println("t2 "+i);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    }




}

}

}

public class test3 implements Runnable{

test2 t2;

public  void run()
{
    synchronized(this){    //synchronized参数指向自己

for(int i=0;i<=10;i++)
{
    System.out.println("t3 "+i);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}
}

}

JVM的synchronized是java并发环境下的同步机制,这是基于锁的,多线程的同步机制对资源进行加锁,使得在同一个时间,只有一个线程可以进行操作,同步用以解决多个线程同时访问时可能出现的问题。
Java中的每个对象都有一个锁(lock),或者叫做监视器(monitor),当一个线程访问某个对象的synchronized方法时,将该对象上锁,其他任何线程都无法再去访问该对象的synchronized方法了(这里是指所有的同步方法,而不仅仅是同一个方法),直到之前的那个线程执行方法完毕后(或者是抛出了异常),才将该对象的锁释放掉,其他线程才有可能再去访问该对象的synchronized方法。

上面代码有点乱,更新了下,原理和结果相同。

public class test {

Thread T2;
Thread TT2;
test2 t2=new test2() ;
test3 t3=new test3();
Thread T3;
Thread TT3;

test()
{

    System.out.println("new test()");
    T2=new Thread(t2);
    TT2=new Thread(t2);

    t2.t3=t3;
    t3.t2=t2;
    T2.start();
    TT2.start();
    T3=new Thread(t3);
    TT3=new Thread(t3);
    T3.start();
    TT3.start();


}





public static void main(String[] args) 
{
    // TODO Auto-generated method stub

new test( );

}

}

public class test2 implements Runnable
{

test3 t3;

public void run()
{

synchronized(t3){

for(int i=0;i<=5;i++)
{


    System.out.println("t2 "+i);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    }




}

}

}
public class test3 implements Runnable{

test2 t2;

public  void run()
{
    synchronized(this){

for(int i=0;i<=10;i++)
{
    System.out.println("t3 "+i);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}
}

}

1、在一个类中,this可以表示该类的当前实例;例如: public class Leaf { private int i = 0; Leaf increment() { i++; return this; } void print() { System.out.println("i = " + i); } public static void main(String[] args) { Leaf......
答案就在这里:java中this的含义?
----------------------你好,人类,我是来自CSDN星球的问答机器人小C,以上是依据我对问题的理解给出的答案,如果解决了你的问题,望采纳。