码迷,mamicode.com
首页 > 其他好文 > 详细

2.1.6synchronized锁重入

时间:2017-12-05 20:09:36      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:调用   char   file   star   可重入   ring   sys   完全   sleep   

关键字在使用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锁重入

标签:调用   char   file   star   可重入   ring   sys   完全   sleep   

原文地址:http://www.cnblogs.com/edison20161121/p/7967435.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!