zoukankan      html  css  js  c++  java
  • 2.2.5synchronized代码间的同步性

    package com.cky.bean;
    
    /**
     * Created by chenkaiyang on 2017/12/6.
     */
    public class ObjectService {
        public void serviceMethodA(){
            synchronized (this) {
                try {
                    System.out.println("a begin time="+System.currentTimeMillis());
                    Thread.sleep(2000);
                    System.out.println("a end time="+System.currentTimeMillis());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
    
        }
    
        public void serviceMethodB(){
            synchronized (this) {
                System.out.println("b begin time="+System.currentTimeMillis());
                System.out.println("b end time="+System.currentTimeMillis());
            }
    
        }
    }
    package com.cky.thread;
    
    import com.cky.bean.ObjectService;
    
    /**
     * Created by chenkaiyang on 2017/12/5.
     */
    public class ThreadA extends Thread{
        private ObjectService service;
        public ThreadA(ObjectService service) {
            super();
            this.service = service;
        }
    
        @Override
        public void run() {
            super.run();
            service.serviceMethodA();
        }
    }
    package com.cky.thread;
    
    import com.cky.bean.ObjectService;
    
    /**
     * Created by chenkaiyang on 2017/12/5.
     */
    public class ThreadB extends  Thread{
        private ObjectService service;
        public ThreadB(ObjectService service) {
            super();
            this.service = service;
        }
    
        @Override
        public void run() {
            super.run();
            service.serviceMethodB();
        }
    }
    package com.cky.test;
    
    import com.cky.bean.ObjectService;
    import com.cky.thread.ThreadA;
    import com.cky.thread.ThreadB;
    
    /**
     * Created by chenkaiyang on 2017/12/6.
     */
    public class Test2 {
        public static void main(String[] args) {
            ObjectService objectService = new ObjectService();
            ThreadA a = new ThreadA(objectService);
            a.setName("a");
            ThreadB b = new ThreadB(objectService);
            b.setName("b");
            a.start();
            b.start();
    
        }
    }
    D:itjdk1.8injava -Didea.launcher.port=7541 "-Didea.launcher.bin.path=D:itideaIntelliJ IDEA 2016.3.3in" -Dfile.encoding=UTF-8 -classpath "D:itjdk1.8jrelibcharsets.jar;D:itjdk1.8jrelibdeploy.jar;D:itjdk1.8jrelibextaccess-bridge-64.jar;D:itjdk1.8jrelibextcldrdata.jar;D:itjdk1.8jrelibextdnsns.jar;D:itjdk1.8jrelibextjaccess.jar;D:itjdk1.8jrelibextjfxrt.jar;D:itjdk1.8jrelibextlocaledata.jar;D:itjdk1.8jrelibext
    ashorn.jar;D:itjdk1.8jrelibextsunec.jar;D:itjdk1.8jrelibextsunjce_provider.jar;D:itjdk1.8jrelibextsunmscapi.jar;D:itjdk1.8jrelibextsunpkcs11.jar;D:itjdk1.8jrelibextzipfs.jar;D:itjdk1.8jrelibjavaws.jar;D:itjdk1.8jrelibjce.jar;D:itjdk1.8jrelibjfr.jar;D:itjdk1.8jrelibjfxswt.jar;D:itjdk1.8jrelibjsse.jar;D:itjdk1.8jrelibmanagement-agent.jar;D:itjdk1.8jrelibplugin.jar;D:itjdk1.8jrelib
    esources.jar;D:itjdk1.8jrelib
    t.jar;F:springboot	hreaddemooutproduction	hreaddemo;D:itideaIntelliJ IDEA 2016.3.3libidea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.test.Test2
    a begin time=1512569404772
    a end time=1512569406772
    b begin time=1512569406772
    b end time=1512569406772

    结果分析:当一个线程访问object的一个同步代码块时,其他线程对同一个object的所有其他的同步代码块将被阻塞,这说明同步代码块使用的是对象监视

  • 相关阅读:
    设置lable内容不上下居中
    iOS中webView加载URL需要处理特殊字符
    搞一个app需要多久?
    戏说HTML5
    限制UITextField/UITextView的输入字数与中文输入之后的英文换行问题
    iOS6以后的单个控制器横竖屏显示以及旋转屏控制技巧,附带iOS8以后显示电池状态栏
    纯命令行教你Cocoapods的安装和使用
    iOS开发之各种动画各种页面切面效果
    UITextView/UITextField检测并过滤Emoji表情符号
    类里面的大括号{}加载顺序
  • 原文地址:https://www.cnblogs.com/edison20161121/p/7989485.html
Copyright © 2011-2022 走看看