zoukankan      html  css  js  c++  java
  • 2.2.6验证同步代码块时锁定当前对象的

    和synchronized方法一样,synchronized(this)代码块也是锁定当前对象的。

    package com.cky.utils;
    
    /**
     * Created by chenkaiyang on 2017/12/6.
     */
    public class Task {
    
        public  void otherMethod() {
            System.out.println("othermethod start");
        }
        public void serviceMethodA(){
            synchronized (this) {
                for (int i = 0; i < 100; i++) {
                    System.out.println("同步"+ Thread.currentThread().getName() +" i="+(i+1));
                }
            }
    
        }
    }
    package com.cky.thread;
    
    import com.cky.utils.Task;
    
    /**
     * Created by chenkaiyang on 2017/12/5.
     */
    public class ThreadA extends Thread{
        private Task service;
        public ThreadA(Task service) {
            super();
            this.service = service;
        }
    
        @Override
        public void run() {
            super.run();
            service.serviceMethodA();
        }
    }
    package com.cky.thread;
    
    import com.cky.utils.Task;
    
    /**
     * Created by chenkaiyang on 2017/12/5.
     */
    public class ThreadB extends  Thread{
        private Task service;
        public ThreadB(Task service) {
            super();
            this.service = service;
        }
    
        @Override
        public void run() {
            super.run();
            service.otherMethod();
        }
    }
    package com.cky.test;
    
    import com.cky.thread.ThreadA;
    import com.cky.thread.ThreadB;
    import com.cky.utils.Task;
    
    /**
     * Created by chenkaiyang on 2017/12/6.
     */
    public class Test2 {
        public static void main(String[] args) throws InterruptedException {
            Task task = new Task();
            ThreadA threadA = new ThreadA(task);
            threadA.start();
            ThreadB threadB = new ThreadB(task);
            threadB.start();
    
        }
    }
    同步Thread-0 i=5
    同步Thread-0 i=6
    同步Thread-0 i=7
    同步Thread-0 i=8
    othermethod start
    同步Thread-0 i=9
    同步Thread-0 i=10

    上面结果执行是异步执行的,下面更改othermethod

    package com.cky.utils;
    
    /**
     * Created by chenkaiyang on 2017/12/6.
     */
    public class Task {
    
        synchronized public  void otherMethod() {
            System.out.println("othermethod start");
        }
        public void serviceMethodA(){
            synchronized (this) {
                for (int i = 0; i < 100; i++) {
                    System.out.println("同步"+ Thread.currentThread().getName() +" i="+(i+1));
                }
            }
    
        }
    }
    同步Thread-0 i=94
    同步Thread-0 i=95
    同步Thread-0 i=96
    同步Thread-0 i=97
    同步Thread-0 i=98
    同步Thread-0 i=99
    同步Thread-0 i=100
    othermethod start
  • 相关阅读:
    layui 获取select下拉选项上自定义的属性
    TP中关联模型的使用
    安卓模仿直播中的闪动(放大缩小)的动画
    Android报错Multiple dex files define Lcom/ta/utdid2/c/a/c
    Date.parse()的坑
    Android应用加固的简单实现方案(二)
    Android应用加固的简单实现方案
    Android中ANR的触发机制-BroadcastReceiver篇
    Android中ANR的触发机制-Service篇
    Application中以标准模式启动Activity报错的原因分析
  • 原文地址:https://www.cnblogs.com/edison20161121/p/8000427.html
Copyright © 2011-2022 走看看