美文网首页
2.1.8同步不具体有继承性

2.1.8同步不具体有继承性

作者: 农家男孩 | 来源:发表于2017-07-02 21:57 被阅读0次

同步不可以继承

/**
 * @author wuyoushan
 * @date 2017/4/11.
 */
public class Main {

    synchronized public void serviceMethod(){
        try {
            System.out.println("int main 下一步sleep begin threadName="+
            Thread.currentThread().getName()+" time="+
            System.currentTimeMillis());
            Thread.sleep(5000);
            System.out.println("int main 下一步sleep end threadName="+
                    Thread.currentThread().getName()+" time="+
                    System.currentTimeMillis());
        }catch(InterruptedException e){
            e.printStackTrace();
        }

    }
}

/**
 * @author wuyoushan
 * @date 2017/4/11.
 */
public class Sub extends Main {

    @Override
    public void serviceMethod() {
        try {
            System.out.println("int sub 下一步sleep begin threadName="+
                    Thread.currentThread().getName()+" time="+
                    System.currentTimeMillis());
            Thread.sleep(5000);
            System.out.println("int sub 下一步sleep end threadName="+
                    Thread.currentThread().getName()+" time="+
                    System.currentTimeMillis());
            super.serviceMethod();
        }catch(InterruptedException e){
            e.printStackTrace();
        }
    }
}

/**
 * @author wuyoushan
 * @date 2017/4/4.
 */
public class ThreadA extends Thread{

    private Sub sub;

    public ThreadA(Sub sub){
        super();
        this.sub=sub;
    }

    @Override
    public void run() {
        super.run();
        sub.serviceMethod();
    }
}

/**
 * @author wuyoushan
 * @date 2017/4/4.
 */
public class ThreadB extends Thread{

    private Sub sub;

    public ThreadB(Sub sub){
        super();
        this.sub=sub;
    }

    @Override
    public void run() {
        super.run();
        sub.serviceMethod();
    }
}

/**
 * @author wuyoushan
 * @date 2017/3/14.
 */
public class Test {
    public static void main(String[] args) {
       Sub subRef=new Sub();
        ThreadA a=new ThreadA(subRef);
        a.setName("A");
        a.start();
        ThreadB b=new ThreadB(subRef);
        b.setName("B");
        b.start();
    }
}

程序的结果:

int sub 下一步sleep begin threadName=A time=1491871403168
int sub 下一步sleep begin threadName=B time=1491871403169
int sub 下一步sleep end threadName=A time=1491871408169
int main 下一步sleep begin threadName=A time=1491871408169
int sub 下一步sleep end threadName=B time=1491871408170
int main 下一步sleep end threadName=A time=1491871413169
int main 下一步sleep begin threadName=B time=1491871413169
int main 下一步sleep end threadName=B time=1491871418169

从上面第一行,第二行可以看到:运行结果是不同步的。由此可知,同步是不能继承,所以还得在子类的方法中添加synchronized关键字,添加以后的运行效果。

int sub 下一步sleep begin threadName=A time=1491872987559
int sub 下一步sleep end threadName=A time=1491872992559
int main 下一步sleep begin threadName=A time=1491872992559
int main 下一步sleep end threadName=A time=1491872997560
int sub 下一步sleep begin threadName=B time=1491872997560
int sub 下一步sleep end threadName=B time=1491873002560
int main 下一步sleep begin threadName=B time=1491873002560
int main 下一步sleep end threadName=B time=1491873007560

摘选自 java多线程核心编程技术-2.1.8

相关文章

  • 2.1.8同步不具体有继承性

    同步不可以继承 程序的结果: 从上面第一行,第二行可以看到:运行结果是不同步的。由此可知,同步是不能继承,所以还得...

  • opacity rgba

    background的属性没有继承性 字体颜色有继承性 opacity 有继承性

  • create-react-app多应用配置

    改写scripts脚本支持多应用(MPA)开发 (use create-react-app@2.1.8) 在不ej...

  • CSS的继承性和层叠性(四)

    一. 继承性 继承:有一些属性,当给自己设置的时候,自己的后代都继承上了,这便是继承性。继承性从自己开始,直到最小...

  • 常用第三方库

    pod 'ReactiveCocoa', '2.1.8' pod 'ReactiveViewModel', '0....

  • WEB前端丨css — 权重、继承性、排版、float

    继承性继承性:在css有某些属性是可以继承下来,如 color,text-xxx,line-height,font...

  • CSS三大特性

    CSS有三大特性,分别是继承性,层叠性,优先级。 继承性 继承性是指指被包在内部的标签将拥有外部标签的样式性,即后...

  • 搭建IM服务 so easy

    数据同步方式 在Web应用上实现数据同步,有"推"、"拉" 两种思路,具体有以下几种方式: 使用HTTP轮循方式说...

  • Java小白系列(二):关键字Synchronized

    一、前言 Synchronized用于线程同步,相信大家都知道,但具体是如何保证线程同步的,有什么要求?今天我们就...

  • TiDB的慢查询日志分析

    TiDB 在 V2.1.8 之后更改了慢日志格式,V2.1.8 之前的版本请看这个文档。 一条不合理的 SQL 语...

网友评论

      本文标题:2.1.8同步不具体有继承性

      本文链接:https://www.haomeiwen.com/subject/wsuocxtx.html