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