标签:dea ide provider 子类 print str ice roc eth
同步不可以继承
package com.cky.bean; /** * Created by chenkaiyang on 2017/12/5. */ public class Father { synchronized public void opeFatherMethod() { try { System.out.println("main begin"); Thread.sleep(5000); System.out.println("main end"); } catch (InterruptedException e) { e.printStackTrace(); } } }
package com.cky.bean; /** * Created by chenkaiyang on 2017/12/5. */ public class Son extends Father{ @Override public void opeFatherMethod() { try { System.out.println("son begin"); Thread.sleep(5000); System.out.println("son end"); } catch (InterruptedException e) { e.printStackTrace(); } } }
package com.cky.thread; import com.cky.bean.Son; /** * Created by chenkaiyang on 2017/12/5. */ public class ThreadA extends Thread{ private Son service; public ThreadA(Son service) { super(); this.service = service; } @Override public void run() { super.run(); service.opeFatherMethod(); } }
package com.cky.thread; import com.cky.bean.Son; /** * Created by chenkaiyang on 2017/12/5. */ public class ThreadB extends Thread{ private Son service; public ThreadB(Son service) { super(); this.service = service; } @Override public void run() { super.run(); service.opeFatherMethod(); } }
package com.cky.test; import com.cky.bean.Son; import com.cky.thread.ThreadA; import com.cky.thread.ThreadB; /** * Created by chenkaiyang on 2017/12/2. */ public class Test { public static void main(String[] args){ Son service = new Son(); ThreadA threadA = new ThreadA(service); threadA.setName("A"); threadA.start(); ThreadB threadB = new ThreadB(service); threadB.setName("B"); threadB.start(); } }
D:\it\jdk1.8\bin\java -Didea.launcher.port=7539 "-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
son begin
son begin
son end
son end
Process finished with exit code 0
从结果可以分析,同步不能继承,所以还得再子类加上synchoized关键字才行。
标签:dea ide provider 子类 print str ice roc eth
原文地址:http://www.cnblogs.com/edison20161121/p/7967447.html