• 2.2.2synchronized同步代码块的使用


    当两个并发线程访问同一个对象object中的synchronized(this)同步代码块时,一段时间内只能有一个线程执行,另一个线程必须等待期执行完才能执行。

    package com.cky.bean;
    
    /**
     * Created by chenkaiyang on 2017/12/6.
     */
    public class ObjectService {
        public void serviceMethodA(){
            try {
                synchronized (this) {
                    System.out.println("begin="+System.currentTimeMillis());
                    Thread.sleep(2000);
                    System.out.println("end="+System.currentTimeMillis());
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
        }
    
    }
    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.serviceMethodA();
        }
    }
    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=7535 "-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
    begin=1512567935564
    end=1512567937564
    begin=1512567937564
    end=1512567939564

    结果表明:方法执行是同步的,但是效果没有提高,下面模拟提高效率

  • 相关阅读:
    php 时间段查询排序分组
    php 导出word
    关于UVM driver的幕后
    三次握手的必要性
    如何卸载360天擎之火绒与天擎相爱相杀
    【虚拟机】VirtualBox设置共享文件夹
    【数据结构与算法】打开转盘锁:使用图的广度优先遍历实现
    【Python】PDF文档导出指定章节为TXT
    【数据结构与算法】不同路径 III:使用哈密尔顿路径算法实现
    【Java】可比较泛型建数组传递报强转类型错误解决方案
  • 原文地址:https://www.cnblogs.com/edison20161121/p/7989474.html
Copyright © 2020-2023  润新知