零号站台

  • 首页
  • 文章归档

  • 搜索
并发编程 data structure java

sychronized锁范围

发表于 2022-03-09 | 0 | 阅读次数 447

sychronized

  • 锁范围

    • 普通同步方法:锁是当前实例对象this,同一个实例中的加锁方法是原子执行的
     /**
      * sychronized范围测试
      */
     @Slf4j(topic = "c.TestLockScope")
     public class TestLockScope {
    
         public static void main(String[] args) throws InterruptedException {
             Resource resource = new Resource();
             //线程1执行5000次加
             Thread t1 = new Thread(() -> {
                 for (int i = 0; i < 5000; i++) {
                     resource.add();
                 }
             },"t1");
    
             //线程2执行5000次减
             Thread t2 = new Thread(() -> {
                 for (int i = 0; i < 5000; i++) {
                     resource.sub();
                 }
             },"t2");
             t1.start();
             t2.start();
             t1.join();
             t2.join();
             //锁是当前resource实例对象,同一个对象在不同线程中的同步方法是原子执行的
             //21:32:25 [main] c.TestLockScope - 结果:0
             log.debug("结果:{}",resource.getNum());
         }
     }
    
      class Resource{
         private int num;
    
         public synchronized void add(){
             num++;
         }
    
         public synchronized void sub(){
             num--;
         }
    
         public synchronized int getNum(){
             return num;
         }
     }
    
    • 静态同步方法:锁是当前类的Class对象,使用Class创建的所有实例中的加锁方法是原子执行的
     /**
      * sychronized范围测试
      */
     @Slf4j(topic = "c.TestLockScope")
     public class TestLockScope {
    
         public static void main(String[] args) throws InterruptedException {
     //        Resource resource1 = new Resource();
     //        Resource resource2 = new Resource();
             //线程1执行5000次加
             Thread t1 = new Thread(() -> {
                 for (int i = 0; i < 5000; i++) {
     //                resource1.add();
                     Resource.add();
                 }
             },"t1");
    
             //线程2执行5000次减
             Thread t2 = new Thread(() -> {
                 for (int i = 0; i < 5000; i++) {
     //                resource2.sub();
                     Resource.sub();
                 }
             },"t2");
             t1.start();
             t2.start();
             t1.join();
             t2.join();
             //锁是当前Resource类的Class对象,Resource类中的静态同步方法在不同线程是原子执行的
             //21:35:55 [main] c.TestLockScope - 结果:0
             log.debug("结果:{}",Resource.getNum());
         }
     }
    
      class Resource{
         private static int num;
    
         public static synchronized void add(){
             num++;
         }
    
         public static synchronized void sub(){
             num--;
         }
    
         public static synchronized int getNum(){
             return num;
         }
     }
    
    • 同步代码块:锁是()里设置的对象,可以设置当前实例对象,也可以设置类的Class对象

      • 锁是当前实例对象
       /**
        * sychronized范围测试
        */
       @Slf4j(topic = "c.TestLockScope")
       public class TestLockScope {
      
           public static void main(String[] args) throws InterruptedException {
               Resource resource = new Resource();
               //线程1执行5000次加
               Thread t1 = new Thread(() -> {
                   for (int i = 0; i < 5000; i++) {
                       resource.add();
                   }
               }, "t1");
      
               //线程2执行5000次减
               Thread t2 = new Thread(() -> {
                   for (int i = 0; i < 5000; i++) {
                       resource.sub();
                   }
               }, "t2");
               t1.start();
               t2.start();
               t1.join();
               t2.join();
               log.debug("结果:{}", resource.getNum());
           }
       }
      
       class Resource {
           private int num;
      
           public void add() {
               synchronized (this) {
                   num++;
               }
           }
      
           public void sub() {
               synchronized (this) {
                   num--;
               }
           }
      
           public int getNum() {
               synchronized (this) {
                   return num;
               }
           }
       }
      
      • 锁是Class对象
       /**
        * sychronized范围测试
        */
       @Slf4j(topic = "c.TestLockScope")
       public class TestLockScope {
      
           public static void main(String[] args) throws InterruptedException {
               Resource resource1 = new Resource();
               Resource resource2 = new Resource();
               //线程1执行5000次加
               Thread t1 = new Thread(() -> {
                   for (int i = 0; i < 5000; i++) {
                       resource1.add();
                   }
               }, "t1");
      
               //线程2执行5000次减
               Thread t2 = new Thread(() -> {
                   for (int i = 0; i < 5000; i++) {
                       resource2.sub();
                   }
               }, "t2");
               t1.start();
               t2.start();
               t1.join();
               t2.join();
               log.debug("结果:{}", resource2.getNum());
           }
       }
      
       class Resource {
           private static int num;
      
           public void add() {
               synchronized (Resource.class) {
                   num++;
               }
           }
      
           public void sub() {
               synchronized (Resource.class) {
                   num--;
               }
           }
      
           public int getNum() {
               synchronized (Resource.class) {
                   return num;
               }
           }
       }
      
  • 本文作者: 妙哉
  • 本文链接: /archives/sychronized锁范围
  • 版权声明: 本博客所有文章除特别声明外,均采用CC BY-NC-SA 3.0 许可协议。转载请注明出处!
# java # 并发编程
稀疏数组
  • 文章目录
  • 站点概览
妙哉

妙哉

2 日志
4 分类
3 标签
RSS
Creative Commons
友链
  • 士子☀的博客
© 2023 妙哉
京ICP备2020043543号-1