synchronized同步方法

本文详细介绍了Java中的synchronized同步方法,包括锁对象、同步特性和缺点。锁对象分为先持有先运行和锁对象同一性,同步方法具有互斥性、可重入性、自动释放锁的特性,但不具有继承性,并且是非公平锁。同时,文章指出synchronized方法可能带来的性能问题。
摘要由CSDN通过智能技术生成

2.3. synchronized同步方法

2.3.1. synchronized方法的锁对象

1)先持有先运行
Synchronized取得的锁都是对象锁,而不是把一段代码/方法作为锁,因此在多线程中,哪个线程先执行带有synchronized关键字修饰的方法,哪个方法就持有该方法所属对象的锁,其他线程只能呈等待状态。但是,前提是:多个线程访问的是同一个对象,如果多个线程访问的不是同一个对象,JVM则会创建出多个对象锁。
样例代码:

public class MultiObjectLock {
   
    public static void main(String[] args) {
   
        MultiObjectLockService multiObjectService1 = new MultiObjectLockService();
        Thread threadA = new MultiObjectLockThreadA(multiObjectService1);   // threadA线程操作multiObjectService1中的synchronized锁
        threadA.start();
        MultiObjectLockService multiObjectService2 = new MultiObjectLockService();
        Thread threadB = new MultiObjectLockThreadB(multiObjectService2);  // threadB线程操作multiObjectService2中的synchronized锁
        threadB.start();
    }
}

class MultiObjectLockService{
   
    private int num = 0;          // 作为成员变量
    synchronized public void add(String name){
   
        if("A".equals(name)){
   
            num = 1;
            System.out.println("A set over");
            try {
   
                Thread.sleep(1000);
            }catch(InterruptedException e){
   
                e.printStackTrace();
            }
        }else if("B".equals(name)) {
   
            num = 2;
            System.out.println("B set over");
            try {
   
                Thread.sleep(1000);
            }catch(InterruptedException e){
   
                e.printStackTrace();
            }
        }
        System.out.println("名称为" + name + "的num值为" + num);
    }
}


class MultiObjectLockThreadA extends Thread{
   
    private MultiObjectLockService multiObjectService;

    public MultiObjectLockThreadA(MultiObjectLockService multiObjectService){
   
        this.multiObjectService = multiObjectService;
    }

    @Override
    public void run(){
   
        this.multiObjectService.add("A");
    }
}


class MultiObjectLockThreadB extends Thread{
   
    private MultiObjectLockService multiObjectService;

    public MultiObjectLockThreadB(MultiObjectLockService multiObjectService){
   
        this.multiObjectService = multiObjectService;
    }

    @Override
    public void run(){
   
        this.multiObjectService.add("B");
    }
}

2)synchronized方法的锁对象同一
在多线程中,如果多个线程访问的是同一对象,如果一个线程调用了该对象中的某个synchronized关键字修饰的方法,其他线程则不能调用该对象中其他的synchronized关键字修饰的方法,其他线程将进入等待状态,但是其他线程可以调用非synchronized关键字修饰的方法。这是因为synchronized锁定的是整个对象,而多个线程访问的是同一对象。
样例程序:

public class SynchronizedLockEntireObject {
   
    public static void main(String[] args) {
   
        SynchronizedLockEntireObjectService synchronizedLockEntireObjectService = new SynchronizedLockEntireObjectService();
        Thread threadA = new SynchronizedLockEntireObjectThreadA(synchronizedLockEntireObjectService);
        threadA.start();
        Thread threadB = new SynchronizedLockEntireObjectThreadB(synchronizedLockEntireObjectService);
        threadB.start();
    }
}


class SynchronizedLockEntireObjectService{
   
    synchronized public void print1(){
   
        System.out.println("开始运行print1,当前线程名为" + Thread.currentThread().getName());
        try{
   
            Thread.sleep(1000);
            System.out.println("print1结束");
        }catch(InterruptedException e){
   
            e.printStackTrace();
        }
    }

    synchronized public void print2(){
   
        System.out.println("开始运行print2,当前线程名为" + Thread.currentThread().getName());
        try{
   
            Thread.sleep(1000);
            System.out.println("print2结束");
        }catch(InterruptedException e){
   
            e.printStackTrace();
        }
    }
}

class SynchronizedLockEntireObjectThreadA extends Thread{
   
    private SynchronizedLockEntireObjectService synchronizedLockEntireObjectService;

    public SynchronizedLockEntireObjectThreadA(SynchronizedLockEntireObjectService synchronizedLockEntireObjectService){
   
        this.synchronizedLockEntireObjectService = synchronizedLockEntireObjectService;
    }

    @Override
    public void run(){
   
        this.synchronizedLockEntireObjectService.print1();
    }
}


class SynchronizedLockEntireObjectThreadB extends Thread{
   
    private SynchronizedLockEntireObjectService synchronizedLockEntireObjectService;

    public SynchronizedLockEntireObjectThreadB(SynchronizedLockEntireObjectService synchronizedLockEntireObjectService){
   
        this.synchronizedLockEntireObjectService = synchronizedLockEntireObjectService;
    }

    @Override
    public void run(){
   
        this.synchronizedLockEntireObjectService.print2();
    }
}
2.3.2. synchronized同步方法的特性
2.3.2.1. 锁是互斥的

1)功能介绍
synchronized锁是一个互斥锁,不仅读写互斥并且读读也互斥,最多只有一个线程能够获得该锁,当线程A尝试去获得线程B持有的内置锁时,线程A必须等待或者阻塞,直到线程B释放这个锁。

public class SynchronizedLockEntireObject {
   
    public static void main(String[] args) {
   
        SynchronizedLockEntireObjectService synchronizedLockEntireObjectService = new SynchronizedLockEntireObjectService();
        Thread threadA = new SynchronizedLockEntireObjectThreadA(synchronizedLockEntireObjectService);
        threadA.start();
        Thread threadB = new SynchronizedLockEntireObjectThreadB(synchronizedLockEntireObjectService);
        threadB.start();</
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值