Synchronized 静态方法和非静态方法的异同

    xiaoxiao2025-09-13  180

    非静态同步方法

    如果使用synchronized来锁定类中所有的同步非静态方法,只需要使用this作为synchronized块的参数传入synchronized块中

    1 public class Test 2 { 3 public void method1() 4 { 5 synchronized(this) //或者使用Test.this当参数 6 { 7 } 8 } 9 10 public synchronized void method2() 11 { 12 } 13 }

    在上面的代码中的method1使用了synchronized块,method2方法是用了synchronized关键字来定义方法,如果使用同一个Test实例时,这两个方法只要有一个在执行,其他的方法都会因未获得同步锁而被堵塞。除了使用this作为synchronized块的参数,也可以使用Test.this作为synchronized块的参数来达到同样的效果。

     

     

    在内类中使用synchronized块中,this只表示内类,和外类(OuterClass)没有关系。但是内类中的非静态方法和外类的非静态方法也可以同步。如果在内类中加个方法method3也可以使和Test里面的2个方法同步。

    1 public class Test 2 { 3 class InnerClass 4 { 5 public void method3() 6 { 7 synchronized(Test.this){ 8 9 } 10 } 11 } 12 }

    InnerClass的method3方法与Test的method1和method2方法在同一时间内只能有一个方法执行。

    synchronized块不管是正确执行完,还是因为程序出错因异常退出synchronized块,当前的synchronized块所持有的同步锁都会自动释放,因此在使用synchronized块不必担心同步锁的问题。

     


     

     

    静态同步方法

      在调用静态方法时,对象实例不一定被创建,因此,就不能使用this来同步静态方法,而必须使用Class对象来同步静态方法。

    方法一:使用类名.class作为参数传入synchronized块

    1 public class Test{ 2 pubic static void method1(){ 3 synchronized(Test.class){} 4 } 5 public static synchronized void method2(){} 6 }

    方法二:通过实例的getClass()方法获取class对象

    1 public class Test{ 2 public static void method1(){ 3 Test test = new Test(); 4 synchronized(test.getclass()){} 5 } 6 public static synchronized void method2(){} 7 }

     

    注意一个类的所有实例通过getClass方法得到的都是同一个Class对象。我们也可以通过class使不同类的静态方法同步。

    1 //Test类中的方法和Test1类中方法同步。 2 public class Test1{ 3 public static void method1(){ 4 synchronized(Test.class){ 5 } 6 } 7 }

     


     

     

    总结

    静态方法属于类,同步静态方法的锁是加在类上;普通方法属于对象,同步非静态方法的锁是加在那个对象上。

     

    在使用synchronized块来同步方法时,

    非静态方法:用this当做参数;

    静态方法:1、用类名.class当参数 2、通过实例的getClass()方法当参数。

     

    转载请注明原文地址: https://ju.6miu.com/read-1302605.html
    最新回复(0)