zoukankan      html  css  js  c++  java
  • Java 学习笔记之 Synchronized锁对象

    Synchronized锁对象:

    Synchronized取得的锁都是对象锁,而不是把一段代码或方法当作锁,哪个线程执行带synchronized关键字的方法,哪个线程就持有该方法所属对象的锁,那么其他线程只能等待,前提是多个线程访问的是同一个对象。

    实验得出以下结论:

    • A线程先持有object对象的Lock锁,B线程可以以异步的方式调用object对象中的非synchronized类型的方法。
    public class Sync1Object {
        synchronized public void methodA() {
            try {
                System.out.println("begin methodA threadName = "
                        + Thread.currentThread().getName());
                Thread.sleep(5000);
                System.out.println("A end endTime = " + System.currentTimeMillis());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
         public void methodB() {
            try {
                System.out.println("begin methodB threadName = "
                        + Thread.currentThread().getName() + " begin time = "
                        + System.currentTimeMillis());
                Thread.sleep(3000);
                System.out.println("B end");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    
    public class Sync1ThreadA extends Thread{
        private Sync1Object object;
    
        public Sync1ThreadA(Sync1Object object) {
            this.object = object;
        }
    
        @Override
        public void run() {
            super.run();
            object.methodA();
        }
    }
    
    public class Sync1ThreadB extends Thread{
        private Sync1Object object;
    
        public Sync1ThreadB(Sync1Object object) {
            this.object = object;
        }
    
        @Override
        public void run() {
            super.run();
            object.methodB();
        }
    }
    
    public class ThreadRunMain {
        public static void main(String[] args) {
            testSync1Thread();
        }
    
        public static void testSync1Thread(){
            try {
                Sync1Object object = new Sync1Object();
                Sync1ThreadA a = new Sync1ThreadA(object);
                a.setName("A");
                a.start();
                Thread.sleep(100);
                Sync1ThreadB b = new Sync1ThreadB(object);
                b.setName("B");
                b.start();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    运行结果:

    • A线程先持有object对象的Lock锁,B线程如果在这个时候调用object对象中的synchronized类型的方法则需要等待,同步串行执行。
    public class Sync1Object {
        synchronized public void methodA() {
            try {
                System.out.println("begin methodA threadName = "
                        + Thread.currentThread().getName());
                Thread.sleep(5000);
                System.out.println("A end endTime = " + System.currentTimeMillis());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
         synchronized public void methodB() {
            try {
                System.out.println("begin methodB threadName = "
                        + Thread.currentThread().getName() + " begin time = "
                        + System.currentTimeMillis());
                Thread.sleep(3000);
                System.out.println("B end");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    
    public class Sync1ThreadA extends Thread{
        private Sync1Object object;
    
        public Sync1ThreadA(Sync1Object object) {
            this.object = object;
        }
    
        @Override
        public void run() {
            super.run();
            object.methodA();
        }
    }
    
    public class Sync1ThreadB extends Thread{
        private Sync1Object object;
    
        public Sync1ThreadB(Sync1Object object) {
            this.object = object;
        }
    
        @Override
        public void run() {
            super.run();
            object.methodB();
        }
    }
    
    public class ThreadRunMain {
        public static void main(String[] args) {
            testSync1Thread();
        }
    
        public static void testSync1Thread(){
            try {
                Sync1Object object = new Sync1Object();
                Sync1ThreadA a = new Sync1ThreadA(object);
                a.setName("A");
                a.start();
                Thread.sleep(100);
                Sync1ThreadB b = new Sync1ThreadB(object);
                b.setName("B");
                b.start();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    运行结果:

  • 相关阅读:
    25、Base64
    24、AES RSA加密处理记录
    23、获取app所占据的内存
    22、DDMS(转载)
    21、HierarchyViewer使用记录
    kubernetes 操作 serviceaccounts
    删除dashboard
    数据库建库指定UTF-8和jdbc连接字符串
    apt 被卸载
    EasyReport报表工具部署
  • 原文地址:https://www.cnblogs.com/AK47Sonic/p/7710626.html
Copyright © 2011-2022 走看看