synchronized

 

对象锁:synchronized修饰普通方法或者synchronized(this)
如下:

  public class SynTest{
    private synchronized void test1(){}


    private void test2(){
      synchronized(this){}
    }
  }

  SynTest SynTest1=new SynTest()
  Thread t1 = new Thread(SynTest1.test1());
  Thread t2 = new Thread(SynTest1.test2());
  注:相同锁


类锁:静态方法或者修饰一个类的class对象
  public class SynTest{
    private static synchronized void test1(){}
    private void test2(){
      synchronized(SynTest.class){}
    }
  }
  SynTest SynTest1=new SynTest()
  Thread t1 = new Thread(SynTest1.test1());
  Thread t2 = new Thread(SynTest1.test2());
  注:相同锁

 

参考:https://www.cnblogs.com/fuly550871915/p/4890753.html

       https://www.cnblogs.com/CarpenterLee/p/7896361.html

相关文章:

  • 2022-12-23
  • 2021-12-06
  • 2022-12-23
  • 2022-02-12
  • 2021-04-18
  • 2021-11-09
  • 2021-12-20
  • 2021-07-22
猜你喜欢
  • 2021-10-19
  • 2021-07-26
  • 2021-09-18
  • 2022-03-08
  • 2021-12-14
  • 2022-12-23
  • 2021-11-20
相关资源
相似解决方案