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();
        }
    }
}

运行结果:

《Java 学习笔记之 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();
        }
    }

     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();
        }
    }
}

运行结果:

《Java 学习笔记之 Synchronized锁对象》

 

    原文作者:AK47Sonic
    原文地址: https://www.cnblogs.com/AK47Sonic/p/7710626.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞