• java并发(二):初探syncronized


    参考博客

    Java多线程系列--“基础篇”04之 synchronized关键字

    synchronized基本规则

    第一条 当线程访问A对象的synchronized方法和同步块的时候,其他线程无法访问A对象的synchronized方法和同步块
    第二条 当线程访问A对象的synchronized方法和同步块的时候,其他线程可以访问A对象的非synchronized方法和同步块
    第三条 当线程访问A对象的synchronized方法和同步块的时候,其他线程不可以访问A对象其他synchronizedd方法和同步块

    第三条基本原则测试

    
    
    import lombok.extern.slf4j.Slf4j;
    
    @Slf4j
    public class SysDemo3 {
        public static void main(String[] args) {
            final  Count count =new Count();
            Thread t1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    count.synMethodA();
                }
            },"t1");
            Thread t2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    count.synMethodB();
                }
            },"t2");
            t1.start();
            t2.start();
        }
    
    
    }
    @Slf4j
    class Count {
        int countsize =5;
    
        public synchronized void synMethodA(){
            int count =0;
            while(count<countsize){
                log.info("synMethodA, Current thread is : {}, count = {}",Thread.currentThread().getName(),count++);
            }
    
        }
        public synchronized void synMethodB(){
            int count =0;
            while(count<countsize){
                log.info("synMethodB, Current thread is : {}, count = {}",Thread.currentThread().getName(),count++);
            }
    
        }
    }
    
    

    测试结果

    2019-07-25 13:40:44,482   [t1] INFO  Count  - synMethodA, Current thread is : t1, count = 0
    2019-07-25 13:40:44,482   [t1] INFO  Count  - synMethodA, Current thread is : t1, count = 1
    2019-07-25 13:40:44,482   [t1] INFO  Count  - synMethodA, Current thread is : t1, count = 2
    2019-07-25 13:40:44,482   [t1] INFO  Count  - synMethodA, Current thread is : t1, count = 3
    2019-07-25 13:40:44,482   [t1] INFO  Count  - synMethodA, Current thread is : t1, count = 4
    2019-07-25 13:40:44,482   [t2] INFO  Count  - synMethodB, Current thread is : t2, count = 0
    2019-07-25 13:40:44,482   [t2] INFO  Count  - synMethodB, Current thread is : t2, count = 1
    2019-07-25 13:40:44,482   [t2] INFO  Count  - synMethodB, Current thread is : t2, count = 2
    2019-07-25 13:40:44,482   [t2] INFO  Count  - synMethodB, Current thread is : t2, count = 3
    2019-07-25 13:40:44,482   [t2] INFO  Count  - synMethodB, Current thread is : t2, count = 4
    

    说明

    每个对象都有一把同步锁,同步锁是依赖对象存在的。上面的结果说明当A对象的同步方法或者同步块被调用的时候,这把锁
    就在使用者中,其他的使用者调用A对象的同步方法或者同步块的时候,是不会获取到锁的。

    实例锁与全局锁

    实例锁是锁在对象上
    全局锁是锁在类上,static syncronized方法或者同步块

    不同的线程调用一个类的不同的static syncronized方法。

    不同的线程调用一个类的不同的static syncronized方法:x.classSyn1()与y.classSyn2(),x不释放,y是无法运行静态同步方法的。

    测试

    
    import lombok.extern.slf4j.Slf4j;
    
    import static java.lang.Thread.sleep;
    /*
        全局锁的使用,类的static syncronized方法或代码块被线程调用,
        其他线程没有拥有全局锁,无法调用其他的同步方法和同步块
     */
    @Slf4j
    public class SysDemo5 {
        static  class Count {
            static int countsize =5;
    
            public synchronized void synMethodA(){
                int count =0;
                while(count<countsize){
                    log.info("synMethodA, Current thread is : {}, count = {}",Thread.currentThread().getName(),count++);
                    try {
                        sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
    
            }
            public synchronized void synMethodB(){
                int count =0;
    
                while(count<countsize){
                    log.info("synMethodB, Current thread is : {}, count = {}",Thread.currentThread().getName(),count++);
                    try {
                        sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
    
            }
            public static synchronized  void cSynMethodA(){
                int count =0;
                while(count<countsize){
                    log.info("class synMethodA, Current thread is : {}, count = {}",Thread.currentThread().getName(),count++);
                    try {
                        sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            public static synchronized  void cSynMethodB(){
                int count =0;
                while(count<countsize){
                    log.info("class synMethodB, Current thread is : {}, count = {}",Thread.currentThread().getName(),count++);
                    try {
                        sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        public static void main(String[] args) {
            Thread t1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    Count.cSynMethodA();
                }
            },"t1");
            Thread t2 = new Thread(new Runnable() {
                @Override
                public void run() {
                   Count.cSynMethodB();
                }
            },"t2");
            t1.start();
            t2.start();
        }
    }
    

    测试结果

    2019-07-25 17:31:24,358   [t1] INFO  SysDemo5  - class synMethodA, Current thread is : t1, count = 0
    2019-07-25 17:31:24,857   [t1] INFO  SysDemo5  - class synMethodA, Current thread is : t1, count = 1
    2019-07-25 17:31:25,356   [t1] INFO  SysDemo5  - class synMethodA, Current thread is : t1, count = 2
    2019-07-25 17:31:25,855   [t1] INFO  SysDemo5  - class synMethodA, Current thread is : t1, count = 3
    2019-07-25 17:31:26,354   [t1] INFO  SysDemo5  - class synMethodA, Current thread is : t1, count = 4
    2019-07-25 17:31:26,853   [t2] INFO  SysDemo5  - class synMethodB, Current thread is : t2, count = 0
    2019-07-25 17:31:27,351   [t2] INFO  SysDemo5  - class synMethodB, Current thread is : t2, count = 1
    2019-07-25 17:31:27,850   [t2] INFO  SysDemo5  - class synMethodB, Current thread is : t2, count = 2
    2019-07-25 17:31:28,349   [t2] INFO  SysDemo5  - class synMethodB, Current thread is : t2, count = 3
    2019-07-25 17:31:28,847   [t2] INFO  SysDemo5  - class synMethodB, Current thread is : t2, count = 4
    

    x.classSyn1与x.sysn1

    对象的同步方法和类的同步方法互不影响

    测试

    import lombok.extern.slf4j.Slf4j;
    
    import static java.lang.Thread.sleep;
    
    @Slf4j
    public class SysDemo4 {
        static  class Count {
            static int countsize =5;
    
            public synchronized void synMethodA(){
                int count =0;
                while(count<countsize){
                    log.info("synMethodA, Current thread is : {}, count = {}",Thread.currentThread().getName(),count++);
                    try {
                        sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
    
            }
            public synchronized void synMethodB(){
                int count =0;
    
                while(count<countsize){
                    log.info("synMethodB, Current thread is : {}, count = {}",Thread.currentThread().getName(),count++);
                    try {
                        sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
    
            }
            public static synchronized  void cSynMethodA(){
                int count =0;
                while(count<countsize){
                    log.info("class synMethodA, Current thread is : {}, count = {}",Thread.currentThread().getName(),count++);
                    try {
                        sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        public static void main(String[] args) {
            final  Count count =new Count();
            Thread t1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    count.synMethodA();
                }
            },"t1");
            Thread t2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    Count.cSynMethodA();
                }
            },"t2");
            t1.start();
            t2.start();
        }
    
    
    }
    

    测试结果

    2019-07-25 19:04:53,184   [t2] INFO  SysDemo4  - class synMethodA, Current thread is : t2, count = 0
    2019-07-25 19:04:53,199   [t1] INFO  SysDemo4  - synMethodA, Current thread is : t1, count = 0
    2019-07-25 19:04:53,683   [t2] INFO  SysDemo4  - class synMethodA, Current thread is : t2, count = 1
    2019-07-25 19:04:53,699   [t1] INFO  SysDemo4  - synMethodA, Current thread is : t1, count = 1
    2019-07-25 19:04:54,182   [t2] INFO  SysDemo4  - class synMethodA, Current thread is : t2, count = 2
    2019-07-25 19:04:54,198   [t1] INFO  SysDemo4  - synMethodA, Current thread is : t1, count = 2
    2019-07-25 19:04:54,682   [t2] INFO  SysDemo4  - class synMethodA, Current thread is : t2, count = 3
    2019-07-25 19:04:54,697   [t1] INFO  SysDemo4  - synMethodA, Current thread is : t1, count = 3
    2019-07-25 19:04:55,181   [t2] INFO  SysDemo4  - class synMethodA, Current thread is : t2, count = 4
    2019-07-25 19:04:55,197   [t1] INFO  SysDemo4  - synMethodA, Current thread is : t1, count = 4
    
    Process finished with exit code 0
    
    
  • 相关阅读:
    备份一些觉得很有用的网站或者资料或者新的一些属性方法技术
    ios下iphone的plus下
    四川大学2008年数学分析考研试题
    四川大学2007年数学分析考研试题
    四川大学2006年数学分析考研试题
    四川大学2005年数学分析考研试题
    四川大学2003年数学分析考研试题
    四川大学2002年数学分析考研试题
    四川大学2001年数学分析考研试题
    四川大学2000年数学分析考研试题
  • 原文地址:https://www.cnblogs.com/JuncaiF/p/11246826.html
Copyright © 2020-2023  润新知