Java中的synchronized关键字,用于修饰需要同步的方法或需要同步的代码块,默认以当前对象作为锁的对象。
在学习synchronized关键字时,有以下几点注意事项:
- 一把锁只能同时被一个线程获取,没有获得锁的线程只能等待;
- 每个实例都对应有自己的一把锁(this),不同实例之间互不影响;例外:锁对象是*.class以及synchronized修饰的是static方法的时候,所有对象公用同一把锁;
- synchronized修饰的方法,无论方法正常执行完毕还是抛出异常,都会释放锁。
这里我们可以注意到,锁加在对象上与锁加在是有区别的,具体如下:
一、对象锁
对象锁包括方法锁(默认锁对象为this,当前实例对象)和同步代码块锁(自己指定对象)。
1.方法锁
方法锁修饰在方法上,多个线程调用同一个对象的同步方法会发生阻塞,调用不同对象的同步方法不会发生阻塞。
public synchronized void objLock1() {
int i = 5;
while (i-- > 0) {
System.out.println(Thread.currentThread().getName() + " : " + i);
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
}
}
}
2.同步代码块锁
同步代码块锁将锁修饰在同步代码块上,传入参数为对象名。
public void objLock2() {
synchronized (this) {
int i = 5;
while (i-- > 0) {
System.out.println(Thread.currentThread().getName() + " : " + i);
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
}
}
}
}
对象锁需要注意,如果是同步代码块所在方法内初始化对象,且以该对象为锁,则锁住的是不同的对象,即方法不会产生阻塞。
public void objLock3() {
String str=new String("lock");//在方法体内,调用一次就实例化一次,多线程访问不会阻塞,因为不是同一个对象,锁是不同的
synchronized (str) {
int i = 5;
while (i-- > 0) {
System.out.println(Thread.currentThread().getName() + " : " + i);
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
}
}
}
}
二、类锁
1.静态方法锁
静态方法锁修饰在静态方法上。
public static synchronized void classLock1() {
int i = 5;
while (i-- > 0) {
System.out.println(Thread.currentThread().getName() + " : " + i);
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
}
}
}
2.同步代码块锁
同步代码块锁将锁修饰在同步代码块上,传入参数为类名。
public void classLoack2() {
synchronized (test.class) {
int i = 5;
while (i-- > 0) {
System.out.println(Thread.currentThread().getName() + " : " + i);
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
}
}
}
}