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

2.1.8同步不具有继承性

时间:2017-12-05 20:08:02      阅读:195      评论:0      收藏:0      [点我收藏+]

标签: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关键字才行。

2.1.8同步不具有继承性

标签:dea   ide   provider   子类   print   str   ice   roc   eth   

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

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