关键字在使用synchronized时,当线程得到一个对象锁时,这时这个线程再次请求此对象锁是可以拿到的,也就说明同步方法之间是可以进行互相调用的,
package com.cky.bean; /**
* Created by chenkaiyang on 2017/12/5.
*/
public class Service {
synchronized public void service1() {
System.out.println("service1");
service2();
} synchronized public void service2() {
System.out.println("service2");
service3();
} synchronized public void service3() {
System.out.println("service3");
}
}
package com.cky.thread; import com.cky.bean.Service; /**
* Created by chenkaiyang on 2017/12/5.
*/
public class MyThread extends Thread{
@Override
public void run() {
super.run();
Service service = new Service();
service.service1();
}
}
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
结果说明:当存在父子类继承关系时,子类完全可以通过可重入锁调用父类同步方法