2.1.6synchronized锁重入

Posted 成功的路上总是离不开贵人的帮助,名师的指点和小人的刺激。

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2.1.6synchronized锁重入相关的知识,希望对你有一定的参考价值。

关键字在使用synchronized时,当线程得到一个对象锁时,这时这个线程再次请求此对象锁是可以拿到的,也就说明同步方法之间是可以进行互相调用的,

 1 package com.cky.bean;
 2 
 3 /**
 4  * Created by chenkaiyang on 2017/12/5.
 5  */
 6 public class Service {
 7    synchronized public void service1() {
 8         System.out.println("service1");
 9         service2();
10     }
11 
12     synchronized public void service2() {
13         System.out.println("service2");
14         service3();
15     }
16 
17     synchronized public void service3() {
18         System.out.println("service3");
19     }
20 }
 1 package com.cky.thread;
 2 
 3 import com.cky.bean.Service;
 4 
 5 /**
 6  * Created by chenkaiyang on 2017/12/5.
 7  */
 8 public class MyThread extends Thread{
 9     @Override
10     public void run() {
11         super.run();
12         Service service = new Service();
13         service.service1();
14     }
15 }
package com.cky.test;

import com.cky.thread.MyThread;

/**
 * Created by chenkaiyang on 2017/12/2.
 */
public class Test {
    public static void main(String[] args){
        MyThread myThread = new MyThread();
        myThread.start();

    }
}
D:\it\jdk1.8\bin\java -Didea.launcher.port=7532 "-Didea.launcher.bin.path=D:\it\idea\IntelliJ IDEA 2016.3.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\it\jdk1.8\jre\lib\charsets.jar;D:\it\jdk1.8\jre\lib\deploy.jar;D:\it\jdk1.8\jre\lib\ext\access-bridge-64.jar;D:\it\jdk1.8\jre\lib\ext\cldrdata.jar;D:\it\jdk1.8\jre\lib\ext\dnsns.jar;D:\it\jdk1.8\jre\lib\ext\jaccess.jar;D:\it\jdk1.8\jre\lib\ext\jfxrt.jar;D:\it\jdk1.8\jre\lib\ext\localedata.jar;D:\it\jdk1.8\jre\lib\ext\nashorn.jar;D:\it\jdk1.8\jre\lib\ext\sunec.jar;D:\it\jdk1.8\jre\lib\ext\sunjce_provider.jar;D:\it\jdk1.8\jre\lib\ext\sunmscapi.jar;D:\it\jdk1.8\jre\lib\ext\sunpkcs11.jar;D:\it\jdk1.8\jre\lib\ext\zipfs.jar;D:\it\jdk1.8\jre\lib\javaws.jar;D:\it\jdk1.8\jre\lib\jce.jar;D:\it\jdk1.8\jre\lib\jfr.jar;D:\it\jdk1.8\jre\lib\jfxswt.jar;D:\it\jdk1.8\jre\lib\jsse.jar;D:\it\jdk1.8\jre\lib\management-agent.jar;D:\it\jdk1.8\jre\lib\plugin.jar;D:\it\jdk1.8\jre\lib\resources.jar;D:\it\jdk1.8\jre\lib\rt.jar;F:\springboot\threaddemo\out\production\threaddemo;D:\it\idea\IntelliJ IDEA 2016.3.3\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.test.Test
service1
service2
service3

Process finished with exit code 0

可重入锁的概念:

自己可以再次获取自己的内部锁,譬如有一条线程获得了某个对象的对象锁,此时这个对象锁还没释放,当其要再次获得这个对象锁的时候还能获取,如果不能获取,则会造成死锁。

package com.cky.bean;

/**
 * Created by chenkaiyang on 2017/12/5.
 */
public class Father {
    public int i=10;
    synchronized public void opeFatherMethod() {
        try {
            i--;
            System.out.println("main print i=" + i);
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
package com.cky.bean;

/**
 * Created by chenkaiyang on 2017/12/5.
 */
public class Son extends  Father{
    synchronized  public void opereSonMethod(){
        try {
            while(i > 0) {
                i--;
                System.out.println("sub print i="+i);
                Thread.sleep(2000);
                this.opeFatherMethod();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
package com.cky.thread;

import com.cky.bean.Son;

/**
 * Created by chenkaiyang on 2017/12/5.
 */
public class MyThread extends Thread{
    @Override
    public void run() {
        super.run();
        Son service = new Son();
        service.opereSonMethod();
    }
}
package com.cky.test;

import com.cky.thread.MyThread;

/**
 * Created by chenkaiyang on 2017/12/2.
 */
public class Test {
    public static void main(String[] args){
        MyThread myThread = new MyThread();
        myThread.start();

    }
}
D:\it\jdk1.8\bin\java -Didea.launcher.port=7536 "-Didea.launcher.bin.path=D:\it\idea\IntelliJ IDEA 2016.3.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\it\jdk1.8\jre\lib\charsets.jar;D:\it\jdk1.8\jre\lib\deploy.jar;D:\it\jdk1.8\jre\lib\ext\access-bridge-64.jar;D:\it\jdk1.8\jre\lib\ext\cldrdata.jar;D:\it\jdk1.8\jre\lib\ext\dnsns.jar;D:\it\jdk1.8\jre\lib\ext\jaccess.jar;D:\it\jdk1.8\jre\lib\ext\jfxrt.jar;D:\it\jdk1.8\jre\lib\ext\localedata.jar;D:\it\jdk1.8\jre\lib\ext\nashorn.jar;D:\it\jdk1.8\jre\lib\ext\sunec.jar;D:\it\jdk1.8\jre\lib\ext\sunjce_provider.jar;D:\it\jdk1.8\jre\lib\ext\sunmscapi.jar;D:\it\jdk1.8\jre\lib\ext\sunpkcs11.jar;D:\it\jdk1.8\jre\lib\ext\zipfs.jar;D:\it\jdk1.8\jre\lib\javaws.jar;D:\it\jdk1.8\jre\lib\jce.jar;D:\it\jdk1.8\jre\lib\jfr.jar;D:\it\jdk1.8\jre\lib\jfxswt.jar;D:\it\jdk1.8\jre\lib\jsse.jar;D:\it\jdk1.8\jre\lib\management-agent.jar;D:\it\jdk1.8\jre\lib\plugin.jar;D:\it\jdk1.8\jre\lib\resources.jar;D:\it\jdk1.8\jre\lib\rt.jar;F:\springboot\threaddemo\out\production\threaddemo;D:\it\idea\IntelliJ IDEA 2016.3.3\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.test.Test
sub print i=9
main print i=8
sub print i=7
main print i=6
sub print i=5
main print i=4
sub print i=3
main print i=2
sub print i=1
main print i=0

Process finished with exit code 0

结果说明:当存在父子类继承关系时,子类完全可以通过可重入锁调用父类同步方法

以上是关于2.1.6synchronized锁重入的主要内容,如果未能解决你的问题,请参考以下文章

并发编程:锁重入与锁异常

5.并发编程-synchronized 细节说明

Java Thread系列synchronized

Java多线程之深入理解synchronize关键字

重入锁

多线程中synchroniaed锁的特性和功能